List contents and navigate Unix directories | ||
---|---|---|
© May 16, 1996 | B-5 |
The Unix directory system helps you to keep track of your files and to find other files that you require. When you are given a Unix account, you are assigned a home directory. When you first login to Unix, you are "in" your home directory. Your home directory's name is your SFU Computing ID. Each other user also has a home directory.
To see a list of the contents of a directory, type the ls command (short for list).
ls . .. firstfile project1 secondfile
There are 5 items in the current directory (including two named "." and ".." which we'll ignore for now). We can't tell from this listing which items are files and which are directories.
We can learn more about the directory's contents by setting options for the ls command. An option is indicated by a hyphen followed by a letter. Options are separated from the command by a space. For example, adding the -F option to the ls command will produce:
ls -F firstfile project1/ secondfile
Now we can tell that project1 is a directory, because it is marked with a /. If we want to find out what files and directories are in the directory project1, we can specify which directory we want ls to list:
ls -F project1 bibliography chapter1 chapter2
project1 directory contains three files. Another useful option is the -l (letter "ell" not one) option which lists directory contents in long format.
ls -l total 3 -rw------- 1 jsmythe 6 Jul 4 14:23 firstfile drwx------ 2 jsmythe 512 Jul 3 11:26 project1 -rw------- 1 jsmythe 4 Jul 8 09:28 secondfile
The total entry shows how much space, measured in 512-byte (.5K) blocks, is used for these files. After that comes one line for each file.
A few other options for the ls command are:
-d | used with -l option to find the status of a directory, instead of listing the directory's contents |
-s | gives the size of each file in kilobytes (K) |
-t | lists entries sorted by time of most recent modification |
-r | reverses the order of the sort to get reverse alphabetic (ls -r) or oldest first (ls -rt) |
-R | recursively lists contents of any subdirectories |
The -R option for the ls command lists the contents of the current directory and the contents of any subdirectories, as shown below:
ls -R firstfile project1/ secondfile project1: bibliography chapter1 chapter2
Multiple options may be given to the ls command. For example ls -lt shows a long listing with the newest (or most recently changed) files first.
Your Unix account has been set up to automatically include two other options whenever you issue an ls command:
-a | list hidden files (system files; their names begin with a period. "." and ".." above are examples) |
-C | list file names in multiple-column format |
See how-to B-14, Customize Your Unix Environment, for details on changing these pre-set options.
Unix files and directories are organized in a hierarchical structure. At SFU, for example, users' home directories are found together in a directory called home. To allow administration of thousands of Unix accounts, the directory home has been subdivided into several partitions (e.g. faculty1, ugrad1, etc.), but you should always refer to home when describing the location of files and when using pathnames (described below).
The directory home is not the topmost level. The ultimate directory is known as the root directory (written as / ). / contains several directories (as the figure shows).
pwd /home/jsmythe
Reading from right to left, this means: I am in the directory called jsmythe, which is contained in the directory home, which is contained in the root directory (/).
Don't be confused by the two meanings of /. It is both the name of the root directory and the character used to separate directories in pathnames like /home/jsmythe.
It is possible that more than one person could have a file called bibliography. How does Unix distinguish between files with the same name? The full name of each file includes the "path" through the directory hierarchy to that file.
The full names of two different bibliography files might be:
/home/jsmythe/project1/bibliography
and
/home/bbrown/thesis/bibliography
The names of the two bibliography files shown above are absolute pathnames. An absolute pathname starts with a / to represent the root directory, then traces the path through various subdirectories to the file.
Another way to describe a file is by its relative pathname. Relative pathnames do not begin with a /. A relative pathname shows how to get to the file from the current working directory. If we are in the directory jsmythe, the relative pathname to our file bibliography is
project1/bibliography
In order to move from our home directory (jsmythe) into the directory called project1, we use the command cd (short for change directory).
cd project1
If you are in the directory data, and wish to "back up" one level to the directory chapter3 (the parent of directory data), you could type
cd chapter3
but a faster way is to use the abbreviation for parent directory (..). There is no space between the 2 periods.
cd ..
Another useful abbreviation is ~ (called tilde). This refers to the user's home directory (jsmythe in our example). The easiest way to change back to your home directory from any directory is to type
cd ~
A third abbreviation (.) means the current working directory. For example, assume that we are in the directory data, and wish to copy the file bibliography from the directory project1 into the directory data. Without using any abbreviations, we would need to type (all on one line):
cp /home/jsmythe/project1/bibliography /home/jsmythe/project1/chapter3/data
Using the abbreviation for our home directory (~) and the abbreviation for the current working directory (.) we would type only:
cp ~/project1/bibliography .
What makes these abbreviations work are the "." and ".." directories that first appeared when we typed ls.