2.9.3. Pathnames: Two Types

Linux for Programmers and Users, Section 3.12

Absolute pathname

Specifies the path with reference to the root of the file system. (Always begins with /, for the root directory).

System configuration files that normally do not change location should be addressed with an absolute pathname.

Relative pathname

Specifies the path with reference to the user’s current location. (cd ../include).

  • Uses . to signify the current directory.
  • Uses .. to signify the parent directory.
  • Used to refer to files that are either impossible or inconvenient to access in an absolute manner.

Both commands and filename arguments can be represented in either form.

Note

A command is also a file and many commands also use filenames as arguments. Absolute and relative pathnames can be used with both.

Note

Consider the grep command which occurs as two versions in /usr/bin and /usr/xpg4/bin on Solaris. If PATH is set up like this:

PATH=/usr/bin:/usr/dt/bin:/usr/xpg4/bin:.

We need to use an absolute pathname to access the version in /usr/xpg4/bin.

Note

A relative pathname may require fewer keystrokes compared to an absolute pathname. For instance, cd .. is often more convenient to use than cd used with an absolute pathname. But should we use relative pathnames in our programs?

We can do that only if the program runs from a specific directory. For instance, the command cp *.c ../backup may not work properly or not at all unless we are positioned in a specific directory. We can’t always ensure that, in which case we need to use an absolute pathname.

If a command named cat exists both in /bin and in the current directory, and this directory occurs after /bin in PATH, then cat foo is not the same as ./cat foo. They run two different programs.