Everything in the UNIX filesystem - files, directories, devices, named pipes, and so on - has two pathnames: absolute and relative. If you know how to find those names, you'll know the best way to locate the file (or whatever) and use it. Even though pathnames are amazingly simple, they're one of the biggest problems beginners have. Studying this article carefully can save you a lot of time and frustration. See Figure 14.1 for an illustration of the UNIX filesystem.
Table 14.1 describes the two kinds of pathnames.
|Absolute Pathnames||Relative Pathnames|
Start at the root directory.
Start at your.
Always start with a slash (
Never start with a slash.
The absolute pathname to some object (file, etc.) is always the same.
The relative pathname to an object depends on your current directory.
To make an absolute pathname:
Start at the root directory (
/) and work down.
Put a slash (
/) after every directory name - though if the path ends
at a directory, the slash after the last name is optional.
For example, to get a listing of the directory highlighted in Figure 14.1, no matter what your current directory is, you'd use an absolute pathname like this:
ls /home/jane/dataSub a b c
To make a relative pathname:
Start at your current directory.
As you move down the tree, away from root, add subdirectory names.
As you move up the tree toward root, add
.. (two dots)
for each directory.
For example, if your current directory is the one shown in Figure 14.1, to get a listing of the Sub subdirectory, use a relative pathname:
ls Subd e f
Without changing your current directory, you can use a relative pathname to read the file d in the Sub subdirectory:
To change the current directory to Jim's home directory, you could use a relative pathname to it:
Using the absolute pathname, /home/jim, might be easier there.
The Figure 14.1, what would be the easiest way to read that file with the pager? It's probably through the symlink:adds a twist to pathnames. What two absolute pathnames would read the file that the symlink points to? The answer: /home/jane/.setup or /work/setups/generic. (The second pathname points directly to the file, so it's a little more efficient.) If your current directory was the one shown in
Remember, when you need to use something in the filesystem, you don't always need to use cd first. Think about using a relative or absolute pathname with the command; that'll almost always work. If you get an error message, check your pathname carefully; that's almost always the problem. If it's hard to visualize the filesystem, a program that makes acan help.