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 ../...

A common “shorthand” notation for the user’s home directory is a tilde, “~”. So, if your username is johndoe, then ~ would be equivalent to the absolute path /home/johndoe/.

Exercise 2.34

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 ../../../?

Exercise 2.35

Which of the following is a relative path?

  • /usr/bin/date

  • bin/date

  • ..

  • /home/student/../student

  • ~/Documents

  • file.txt

Exercise 2.36

Assume your current working directory is /home/student/ros2_ws/src. What would be the relative path to /home/student?

  • ../../..

  • ../..

  • ..

  • /home/student

  • /home/student/

  • ~

  • ./ros2_ws/src

  • ././.

  • ./.

  • .

Solution to Exercise 2.36

../..

Exercise 2.37

What is the shorthand absolute path notation for a projects/ subdirectory in your home directory in the shell?

  • projects

  • ./projects

  • /home/student/projects

  • /projects

  • -/projects

  • ~/projects

Solution to Exercise 2.37

~/projects

Note that we have introduced the concept that, when using the shell, at any moment we “are in” a certain directory. This is called the current working directory. It is possible to change the working directory with shell commands, enabling you to execute commands to inspect or perform file operations in different parts of 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.39

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

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.40

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

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

$ cd

Exercise 2.41

Navigate to the projects directory in your home, and then to /usr/bin. Afterwards, use cd - to go back to the projects directory, and then use cd - again to move directly back to /usr/bin.

{
  "checks": [
    { "type": "commandEvent", "eventType": "builtin.cd",
      "match": { "absPath": "/home/student/projects" },
      "desc": "cd to projects in your home" },
    { "type": "commandEvent", "eventType": "builtin.cd",
      "match": { "absPath": "/usr/bin" },
      "desc": "cd to /usr/bin" },
    { "type": "commandEvent", "eventType": "builtin.cd",
      "match": { "absPath": "/home/student/projects", "form": "previous" },
      "desc": "use cd - to ~/projects" },
    { "type": "commandEvent", "eventType": "builtin.cd",
      "match": { "absPath": "/usr/bin", "form": "previous" },
      "desc": "use cd - to switch back to /usr/bin" },
    { "type": "tabCompletionHint" }
  ]
}

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.42

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.

Exercise 2.43

Starting in /usr/bin, what is your working directory after running cd ../../usr/bin/../bin? Enter the result as an absolute path.

Solution to Exercise 2.43

/usr/bin

Exercise 2.44

List the contents of /usr/share/common-licenses/ in long format, sorted by file size (largest first), in a single command.

{
  "filesystemZip": "bash-exercise-fs/fs-usr-share-common-licenses.zip",
  "checks": [
    { "type": "commandEvent", "eventType": "coreutil.ls",
      "match": { "flags": ["l", "S"], "absPaths": ["/usr/share/common-licenses"] },
      "desc": "use ls with -l and -S flags on /usr/share/common-licenses" }
  ]
}