UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.1 Getting Around the Filesystem Chapter 14
Moving Around in a Hurry
Next: 14.3 What Good Is a Current Directory?
 

14.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 14.1 for an illustration of the UNIX filesystem.

Figure 14.1: A UNIX Filesystem Tree

Figure 14.1

Table 14.1 describes the two kinds of pathnames.

Table 14.1: Absolute and Relative Pathnames
Absolute PathnamesRelative Pathnames

Start at the root directory.

Start at your current directory (1.21).

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 14.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 14.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 (18.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 14.1, what would be the easiest way to read that file with the more (25.3) 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 almost always the problem. If it's hard to visualize the filesystem, a program that makes a diagram of the directory tree (16.19, 16.20) can help.

- JP


Previous: 14.1 Getting Around the Filesystem UNIX Power ToolsNext: 14.3 What Good Is a Current Directory?
14.1 Getting Around the Filesystem Book Index14.3 What Good Is a Current Directory?

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System