You use filenames constantly in Unix. Almost every command covered in this chapter, and most commands in Unix, accept one or more filenames as arguments. Those filenames can be entered in two different forms: as relative paths or as full paths (also called absolute paths ).
The difference between relative and full paths is simple but critical. A path tells you not only the name of a file, but also its location in the file system. A path is like a set of directions telling you how to get somewhere.
Full paths always begin with a / (slash) and give the absolute location of the file in the file system; that is, they give you "directions" to the file starting from the root directory. Every full path is completely unique.
Relative paths never begin with a / , and they give you directions to the file starting from your working directorythat is, from where you are right now. The same relative path can refer to different files, depending on where it is used.
To use a full path as an argument:
To use a relative path as an argument:
To use a more complex relative path:
- ls ../../Developer/Tools
Each .. is a special directory name that means "one step closer to the root directory" (see the sidebar "Two Special Directory Names " for more details). Each .. takes you one step closer to / , so starting from a working directory of /Users/vanilla , the first .. refers to /Users , and the second .. refers to / itself. (The .. path is often said to refer to the parent directory .)
So the path ../../Developer/Tools means, "Starting from the working directory, take two steps toward the root directory and then look for a directory called Developer , and then inside that for Tools."
Having the shell type the filename for you
Now that you have been typing some longer paths, you will be happy to learn about the filename completion feature of some Unix shells (see Chapter 2 to review shells ).
Filename completion is a way of having the shell type most of the filename for you. You can use it whenever you are typing a filename or path that already exists.
To use filename completion:
Type any command that will use a filename as an argument.
For example, to see the long-form listing of contents of the /etc/httpd/users directory, type ls -l (make sure to type a space after the -l ).
Type the beginning of the filename. For example, in this case the beginning would be /e
. Figure 5.7
shows the command line up to this point.
Figure 5.7. To use filename completion, you start by typing the beginning of the filename.
localhost:~ vanilla$ ls -l /e
The shell tries to fill in the rest of the path, or at least as much of the path as is unique. In this case, there is only one entry in /
that begins with e
, so the shell fills in /etc/
and then waits for you to continue ( Figure 5.8
Figure 5.8. When you press , the shell will try to fill in the rest of the filename.
localhost:~ vanilla$ ls -l /etc/
Type a little more of the filename.
In this case, just type an h
, so that your command line now looks like this: ls -l /etc/h
Two Special Directory Names
There are two very special directory names: . and .. (that's a single dot and a double dot).
The . directory name always translates to the full path of the working directory. So if your working directory is /Users/puffball/bin , then the relative path ./script.sh translates to /Users/puffball/bin/script.sh .
Anytime you are using a path, you can use the directory name .. to mean "one step closer to the root directory." So if your working directory is /usr/local/bin , then the path ../../sbin translates to /sbin (each .. goes one step closer to / ).
The .. directory name refers to the parent directory , and you will often see that term in Unix documentation.
The . in script.sh does not have any special meaning. The . and .. have their special meanings only when they are used as a directory name, not as part of a directory or filename.
In this case, there are many entries in /etc/
that begin with h
, so the shell beeps. If you then press
again, the shell displays all the entries that match ( Figure 5.9
). You should see the one you want among the entries listed.
Figure 5.9. If there is more than one possible match when you press , the shell beeps. If you then press , all the available matches are displayed. The list in this figure may be a little different from what you'll get on your system.
localhost:~ vanilla$ ls -l /etc/h hostconfig hostconfig~ hosts.lpd hostconfig.applesaved hosts httpd hostconfig.applesaved2 hosts.equiv localhost:~ vanilla$ ls -l /etc/h
Notice how the shell repeats the partial command line after showing you the possible matches.
Type just enough of the filename to make it unique. In this case, that would be a t , so your command line now looks like this:
ls -l /etc/ht
The shell fills in some more, so now your command line looks like this: ls -l /etc/httpd/
The shell shows you everything in /etc/httpd/
( Figure 5.10
) and repeats the partial command line.
Figure 5.10. Another example of the shell's displaying possible matches after you press and .
localhost:~ vanilla$ ls -l /etc/httpd/ httpd.conf httpd.conf.default mime.types.default httpd.conf.applesaved magic users httpd.conf.applesaved2 magic.default httpd.conf.bak mime.types localhost:~ vanilla$ ls -l /etc/httpd/
A Special Name for Home
The ~ (tilde) character has a special meaning when used on the command line in a path. It translates to "the home directory of the user " whose username follows the ~ . So ~puffball TRanslates to /Users/puffball .
If the ~ is used without a user name, for example, in ~/Documents , then it translates to the home directory of whichever user is running the command.
The actual locations of users' home directories are different on different Unix systems, and in some cases different users on the same system may have their home directories in different places. For example, in Mac OS X, regular users have their home directories in /Users , but the root user's home directory is /var/root .
Using the ~ character lets the shell do the work of figuring out where a user's home directory is, so you can use ~puffball without having to know exactly where puffball's home directory is, and you can use ~/ to refer to your own home directory regardless of your working directory.
Repeat the process of typing a little more of the filename and pressing
until the filename you want is filled in.
In this case, it should be sufficient to type a u
and then press
. That should fill in the whole filename you want: ls -l /etc/httpd/users/
If there are more arguments, type them in.
In this example, there would not be any more arguments. In other cases you might have more filenames or other arguments to add to the command line.
when the command line is complete. Figure 5.11
shows the final result. (You should see a line for each user on your system.)
Figure 5.11. The result of the completed command line. Your output depends on the user accounts you've created on your system.
localhost:~ vanilla$ ls -l /etc/httpd/users/ total 32 -rw-r--r-- 1 root wheel 142 Mar 14 14:50 howard.conf -rw-r--r-- 1 root wheel 207 Mar 14 14:52 matisse.conf -rw-r--r-- 1 root wheel 141 Jan 28 13:16 noway.conf -rw-r--r-- 1 root wheel 143 Dec 26 12:48 vanilla.conf localhost:~ vanilla$
Use filename completion as much as you can, because it will help you avoid misspellings.
The instructions above cover the default shell, bash ; if you are using the tcsh shell, then in step 5, instead of the second you would press .