Chapter 2: Directories and files

Filenames

face Robert Craven

An absolute path to a file or directory begins with the root directory and follows the nested directory structure until the destination is reached. (As we’ve seen, pwd finds the absolute path of the working directory.)

In contrast, relative pathnames start from the working directory.

If your working directory is /usr and you want to move into its sub-directory, bin, you can either use the absolute pathname:

username@MACHINE:/usr$ cd /usr/bin

or the relative pathname:

username@MACHINE:/usr$ cd bin

Given that you’re in /usr, both have the same effect.

Making life easier

Just as ~ refers to your home directory, .. refers to the parent directory.

The parent of /usr/bin, is /usr: it’s the next directory up in the filesystem tree.

username@MACHINE:/usr/bin$ cd ..
username@MACHINE:/usr$

Similarly, . (a single full stop) refers to a directory itself.

Exercise 4

Suppose you’re in /usr/local/lib (i.e., that’s the working directory). Where would you be after each of the following commands (executed one after the other)? Give the absolute pathnames.

cd ../..
cd share
cd python3
cd ~/..

Tab

When typing a filename into the shell, once you have typed enough characters to uniquely select a single directory, file, or command, press Tab. The shell will complete the name for you.

If there are several alternatives, pressing Tab twice will list them all.

Head back to /usr and press Tab twice after typing the following:

username@MACHINE:/usr$ cd s

You’ll see all directories in /usr which begin with an s.