2.4.6. Moving about the file system#

You’ve explored the Linux file system using the graphical user interface in Section 2.3.3. Of course, you can also do this using the shell. We’ll first have to become a bit more specific about how we represent files and directories.

The naming of directories and files is as follows:

  1. a single / at the beginning denotes the root directory;

  2. a name followed by a / indicates a directory;

  3. a name not followed by a / indicates a file.

For example, the command date is stored in /usr/bin/date. There is a subdirectory below the root directory called usr. This directory itself has a subdirectory called bin, and in this directory there is a file called date. We call /usr/bin/date the path to the command date; it’s the path you have to take through the tree to get to the sequence of bytes called date.

The path to date is an example of an absolute path. No matter where you currently are in the tree, you will first have to return to the root (/) and from there go to /usr/bin to find date. It’s also possible to have a relative path. Say you are currently in the directory /usr, then it’s not necessary to go all the way back to the root. Instead, you can simply specify that date is in the bin subdirectory of the directory you’re in. The relative pathname then would be bin/date. Note that there’s no / at the beginning of a relative path.

The relative path above only refered to a file “downward” in the tree, away from the root. However, it’s also possible to refer to higher directories. The first directory above the one you’re currently in is called the parent directory and can be referenced by ... For example, say we are in /usr, then the path to the root directory could be written as ... If we’re in /usr/bin, the path to the root directory is ../...

Exercise 2.31

Starting in /usr/bin, where do you think you will end up if you go to ../../usr? And if you go to ../bin/../../usr/bin/../bin/?

Where do you think you end up if you go to ../../../?

Note that we have introduced a directory we “are in” now. This is called the current working directory. By using commands to change this, you will be able to walk around in the filesystem tree. Unix has a number of commands to do this.

2.4.6.2. Change the working directory#

To change directories, you can use the cd command (short for change directory). It simply takes as argument the place you want to go, e.g.:

$ cd /usr/bin

or

$ cd ..

Exercise 2.32

Use the cd command to verify the answers you gave to Exercise 2.31.

Two final useful properties of cd are:

  • cd without any options will take you back to your home directory;

  • cd - will take you back to the directory you were last.

Exercise 2.33

$ cd /bin
$ cd /usr/bin
$ cd -

Where should you now be? Verify this using $ pwd. Go back to your home directory by typing:

$ cd

2.4.6.3. Listing the content of a directory#

To have a look at your directory, use the command ls (short for “list files”):

$ ls

This will print all files and directories in the directory you’re currently working in. You can’t see any files and directories above or below.

ls is probably one of the commands with the largest number of options in all of Unix . The most important one lets you specify another directory to look in:

$ ls /bin

Now ls shows you which files are present in the directory /bin, rather than the directory you’re currently in.

Exercise 2.34

Inspect the ls man page and try some of the options, such as -lS and -lt.

We will look at more advanced uses of ls later in the manual in Section 2.5.1.