Navigating Directories with UNIX

This tutorial document assumes you've already read the first document in this series entitled "Getting Started with UNIX".

Contents


Introduction

A directory in UNIX may contain any number of files and/or subdirectories. The subdirectories themselves may contain additional files and directories, and so on. In this way, megabytes of files may be organized in hierarchical fashion. In this tutorial, we'll learn how to navigate this maze of UNIX directories.


Your home directory

Once you start moving around in UNIX, it's important to know where you are at any given point in time. At the UNIX prompt, type the following commands (not including the percent sign % or the comments after the pound sign %):
   # change directory to home directory:
   % cd
   # output the present working directory:
   % pwd
These two commands will output the path to your home directory, which was assigned to you at the time your UNIX account was created. When I type the above commands on my UNIX system, I get the output
   /home/trscavo
which means my personal directory "trscavo" resides in the system directory "/home". Okay, that's good to know, and we'll use that information in the next section. For now, let's concentrate on our home directory, since that's where we'll be doing most of our work.

We've just made our home directory the present working directory (by typing the cd command). The present working directory is sometimes called the current directory. At the UNIX prompt, type the command

   # list the files in the current directory:
   % ls
to get a list of files and folders in the current directory (i.e., your home directory). When I type the ls command at the UNIX command line, I get the output
   Mail/          bin/           mail/          public_html/
   News/          for.judy.gray  mbml.archive@  tmp/
which tells me I have one file ("for.judy.gray"), six directories ("Mail", "News", "bin", "mail", "public_html", and "tmp"), and one link ("mbml.archive") in my home directory. (For the time being, ignore the link.) Of course, your home directory listing will look different.

Note: If the ls command on your system does not append a slash / to directory names and an at-sign @ to links, try

   % ls -F
instead.

Now let's look inside one of these directories and see what's there:

   # list the files in the mail directory:
   % ls mail
   saved-messages  sent-mail
My "mail" directory evidently contains two files, one called "saved-messages" and another called "sent-mail". (These files are used by an e-mail client called pine.) Now let's actually make the "mail" directory our current directory:
   # change directory to the mail directory:
   % cd mail
   # output the present working directory:
   % pwd
   /home/trscavo/mail
   # list the files in the current directory:
   % ls
   saved-messages  sent-mail
The last command is simply typed ls because the "mail" directory is now the current directory.

Now let's check out another directory in my home directory:

   # change directory to home directory:
   % cd
   # output the present working directory:
   % pwd
   /home/trscavo
   # change directory to public_html directory:
   % cd public_html
   # output the present working directory:
   % pwd
   /home/trscavo/public_html
   # list the files in the current directory:
   % ls
   education/        education.html    mirror-list.html
There are two files ("education.html" and "mirror-list.html") and one directory ("education") in my "public_html" directory. I wonder what's in the "education" directory:
   # list the files in the education directory:
   % ls education
   edu-administrators.html  edu-teachers.html        gov-state.html
   edu-parents.html         gov-federal.html
   edu-students.html        gov-local.html
There are seven files in my "education" directory, all having file extension .html (this means they are hypertext markup language (HTML) documents, which we'll have more to say about in a later lesson). To get to the "education" directory from our home directory, we changed one directory at a time, but we could have gone there directly:
   # change directory to home directory:
   % cd
   % pwd
   /home/trscavo
   % cd public_html/education
   % pwd
   /home/trscavo/public_html/education
   % ls
   edu-administrators.html  edu-teachers.html        gov-state.html
   edu-parents.html         gov-federal.html
   edu-students.html        gov-local.html
So if you know exactly where you want to go, you can get there quickly by typing the complete path, as it's called.

Here's a shortcut I use all the time:

   % pwd
   /home/trscavo/public_html/education
   # change directory to parent directory:
   % cd ..
   % pwd
   /home/trscavo/public_html
   % ls
   education/        education.html    mirror-list.html
   # change directory to parent directory:
   % cd ..
   % pwd
   /home/trscavo
   % ls
   Mail/          bin/           mail/          public_html/
   News/          for.judy.gray  mbml.archive@  tmp/
Two consecutive dots denote the so-called parent directory. In other words, the command cd .. is an easy way to back up one level in the directory hierarchy without having to specify a directory name.


Contents

System directories

We're not going to venture too far from our home directory, but let me give you a feel for the amount of stuff there is on your UNIX host. Try this experiment:
   # change directory to root directory:
   % cd /
   % pwd
   /
   % ls
   System.map   cdrom/       lib/         root/        usr/
   System.old   dev/         lost+found/  sbin/        var/
   bin/         etc/         mnt/         swap         vmlinuz
   boot/        home/        proc/        tmp/         vmlinuz.old
You are in what's called the "root" directory, denoted by the single character "/". It is the highest level UNIX directory, and it contains everything! You're welcome to explore further, if you like, but I think I'll stop here. Note that some system files and directories are protected, so don't be surprised if UNIX turns you back at some point.


Contents

Command variations

Most UNIX commands can be made to behave differently by means of switches (or options). Let me give an example of a useful option for the ls command:
   % ls
   Mail/          bin/           mail/          public_html/
   News/          for.judy.gray  mbml.archive@  tmp/
   # long list of files in current directory:
   % ls -l
   total 9
   drwxr-xr-x   2 trscavo  users        1024 Jan  1 12:27 Mail/
   drwxr-xr-x   2 trscavo  users        1024 Jan  1 12:27 News/
   drwxr-xr-x   2 trscavo  users        1024 Jan  4 11:09 bin/
   -rw-r--r--   1 trscavo  users        2523 Jan 12 09:31 for.judy.gray
   drwx------   2 trscavo  users        1024 Jan 22 17:32 mail/
   lrwxrwxrwx   1 trscavo  users          43 Jan 18 05:06 mbml.archive -> /usr/local/lib/majordomo/lists/mbml.archive/
   drwxr-xr-x   3 trscavo  users        1024 Jan  3 13:13 public_html/
   drwxr-xr-x   2 trscavo  users        1024 Jan 17 12:58 tmp/
As you can see, the output of the ls command is quite different from the modified command ls -l (the "l" stands for "long", by the way). Instead of a brief horizontal listing of files, one gets a lengthy vertical (or "long") listing. Moreover, the long listing contains considerably more information (the details of which I will ignore for now).

Here's another useful option for the ls command:

   # list of all files in current directory:
   % ls -a
   ./               .lastlogin       .pinerc          bin/
   ../              .less            .plan            for.judy.gray
   .addressbook     .lessrc          .signature       mail/
   .addressbook.lu  .login           .tin/            mbml.archive@
   .forward         .newsrc          Mail/            public_html/
   .kermrc          .oldnewsrc       News/            tmp/
The option -a stands for "all", which is exactly what you get: a listing of all files and folders in the current directory. Evidently, there are many more files and folders in my home directory than we had first imagined! Yes, that's true. What we've been overlooking is the hidden files and folders, that is, those files and folders whose name begins with a dot. Such a file or folder is normally hidden from view...until one types ls -a at the UNIX command line.

Options may be combined. For example, the command

   # long list of all files in current directory:
   % ls -al
   total 49
   drwxr-xr-x   9 trscavo  users        1024 Jan 22 17:32 ./
   drwxr-xr-x 1278 root     root        20480 Jan 22 14:25 ../
   -rw-r--r--   1 trscavo  users           0 Jan  1 12:26 .addressbook
   -rw-r--r--   1 trscavo  users        1257 Jan  1 12:26 .addressbook.lu
   -rw-r--r--   1 trscavo  users          16 Jan  1 12:13 .forward
   -rw-r--r--   1 trscavo  users         164 Jan  1 11:31 .kermrc
   -rw-r--r--   1 trscavo  users           0 Jan 22 17:30 .lastlogin
   -rw-r--r--   1 trscavo  users          34 Jan  1 11:31 .less
   -rw-r--r--   1 trscavo  users         114 Jan  1 11:31 .lessrc
   -rw-r--r--   1 trscavo  users          43 Jan 12 14:40 .login
   -rw-r--r--   1 trscavo  users         129 Jan  1 12:30 .newsrc
   -rw-r--r--   1 trscavo  users         129 Jan 22 17:25 .oldnewsrc
   -rw-r--r--   1 trscavo  users        5433 Jan 22 17:32 .pinerc
   -rw-r--r--   1 trscavo  users         309 Jan  1 12:14 .plan
   -rw-r--r--   1 trscavo  users          62 Jan 22 11:15 .signature
   drwxr-xr-x   4 trscavo  users        1024 Jan 22 17:25 .tin/
   drwxr-xr-x   2 trscavo  users        1024 Jan  1 12:27 Mail/
   drwxr-xr-x   2 trscavo  users        1024 Jan  1 12:27 News/
   drwxr-xr-x   2 trscavo  users        1024 Jan  4 11:09 bin/
   -rw-r--r--   1 trscavo  users        2523 Jan 12 09:31 for.judy.gray
   drwx------   2 trscavo  users        1024 Jan 22 17:32 mail/
   lrwxrwxrwx   1 trscavo  users          43 Jan 18 05:06 mbml.archive -> /usr/local/lib/majordomo/lists/mbml.archive/
   drwxr-xr-x   3 trscavo  users        1024 Jan  3 13:13 public_html/
   drwxr-xr-x   2 trscavo  users        1024 Jan 17 12:58 tmp/
combines the effects of the -a and -l options, giving a long listing of all files in the current directory. Similarly, here's a command that lists the files and directories in my "mail" directory:
   # long list of all files in mail directory:
   % ls -al mail
   total 94
   drwx------   2 trscavo  users        1024 Jan 22 17:32 ./
   drwxr-xr-x   9 trscavo  users        1024 Jan 22 17:32 ../
   -rw-------   1 trscavo  users           0 Jan  1 12:26 saved-messages
   -rw-------   1 trscavo  users       93076 Jan 22 17:32 sent-mail
When we looked at my "mail" directory earlier with the ls mail command, there were only two files ("saved-messages" and "sent-mail"). Now it appears that there are four! The two extra entries in the above listing ("." and "..") are shorthand notation for the current directory (".") and the parent directory (".."). We've used the latter before, when we typed cd .. to retrace our steps in the hierarchical directory structure of UNIX.

Now here's a more powerful option. Type the following command at the UNIX command line:

   # recursively list all files in all directories:
   % ls -R
   Mail/          bin/           mail/          public_html/
   News/          for.judy.gray  mbml.archive@  tmp/
   
   Mail:
   
   News:
   
   bin:
   freeside.users  get.users*      user.addresses
   
   mail:
   saved-messages  sent-mail
   
   public_html:
   education/        education.html    mirror-list.html
   
   public_html/education:
   edu-administrators.html  edu-teachers.html        gov-state.html
   edu-parents.html         gov-federal.html
   edu-students.html        gov-local.html
   
   tmp:
   resume.aux   resume.dvi   resume.html  resume.log   resume.ps    resume.tex
The option -R stands for "recursive" and causes the ls command to give a listing of all files and directories in the current directory, as well as all subdirectories no matter how deep.


Contents

Command summary

   # output the present working directory:
   % pwd
   # list the files in the current directory:
   % ls
   # list the files in directory:
   % ls directory
   # change directory to home directory:
   % cd
   # change directory to directory:
   % cd directory
   # change directory to parent directory:
   % cd ..
   # long list of files in current directory:
   % ls -l
   # list of all files in current directory:
   % ls -a
   # long list of all files in current directory:
   % ls -al
   # long list of all files in directory:
   % ls -al directory
   # recursively list all files in all directories:
   % ls -R
The next in this series of tutorial documents is entitled "Manipulating Files with UNIX".