start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

31.2. Using Relative and Absolute Pathnames

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 31-1 for an illustration of the Unix filesystem.

Figure 31-1

Figure 31-1. A Unix filesystem tree

Table 31-1 describes the two kinds of pathnames.

Table 31-1. Absolute and relative pathnames

Absolute pathnames

Relative pathnames

Start at the root directory.

Start at your current directory (Section 1.16).

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:

For example, to get a listing of the directory highlighted in Figure 31-1, no matter what your current directory is, you'd use an absolute pathname like this:

% ls /home/jane/data
Sub    a    b     c

To make a relative pathname:

For example, if your current directory is the one shown in Figure 31-1, to get a listing of the Sub subdirectory, use a relative pathname:

% ls Sub
d     e     f

Without changing your current directory, you can use a relative pathname to read the file d in the Sub subdirectory:

% cat Sub/d

To change the current directory to Jim's home directory, you could use a relative pathname to it:

% cd ../../jim

Using the absolute pathname, /home/jim, might be easier there.

The symbolic link (Section 10.4) 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 Figure 31-1, what would be the easiest way to read that file with the more pager? It's probably through the symlink:

% more ../.setup

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 usually the problem.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.