Navigating Files and Directories

Overview

Teaching: 0 min
Exercises: 0 min
Questions
  • How can I move around on my computer?

  • How can I see what files and directories I have?

  • How can I specify the location of a file or directory on my computer?

Objectives
  • Explain the similarities and differences between a file and a directory.

  • Translate an absolute path into a relative path and vice versa.

  • Construct absolute and relative paths that identify specific files and directories.

  • Use options and arguments to change the behaviour of a shell command

  • Demonstrate the use of tab completion, and explain its advantages.

The part of the operating system responsible for managing files and directories is called the file system. It organizes our data into files, which hold information, and directories (also called ‘folders’), which hold files or other directories.

Several commands are frequently used to create, inspect, rename, and delete files and directories. To start exploring them, we’ll go to our open shell window.

First let’s find out where we are by running a command called pwd (which stands for ‘print working directory’). Directories are like places - at any time while we are using the shell we are in exactly one place, called our current working directory. Commands mostly read and write files in the current working directory, i.e. ‘here’, so knowing where you are before running a command is important. pwd shows you where you are:

$ pwd

Moving around files and directories

$ cd 
$ cd ..

.. is a special directory name meaning “the directory containing this one”, or more succinctly, the parent of the current directory.

The basic commands for navigating the filesystem on your computer: pwd, ls and cd. Let’s explore some variations on those commands.

What happens if you type cd on its own, without giving a directory?

$ cd

How can you check what happened? pwd gives us the answer! It turns out that cd without an argument will return you to your home directory, which is great if you’ve gotten lost in your own filesystem.

$ pwd
/homes/kpegion

Let’s get some data and examples to work with for this lesson:

First, copy the file /homes/kpegion/classes/fa2020/data-shell.zip to your home directory

$ cd
$ cp /homes/kpegion/classes/fa2020/data-shell.zip .

Unizp the file:

$ unzip data-shell.zip

Let’s go explore the directories we copied:

$ cd data-shell/data

Check that we’ve moved to the right place by running pwd and ls -F

Here we used the relative path to specify the directory. When you use a relative path with a command like ls or cd, it tries to find that location from where we are, rather than from the root of the file system.

However, it is possible to specify the absolute path to a directory by including its entire path from the root directory, which is indicated by a leading slash. The leading / tells the computer to follow the path from the root of the file system, so it always refers to exactly one directory, no matter where we are when we run the command.

This allows us to move to our data-shell directory from anywhere on the filesystem (including from inside data). To find the absolute path we’re looking for, we can use pwd and then extract the piece we need to move to data-shell.

$ pwd
/homes/kpegion/data-shell/data
$ cd /homes/kpegion/data-shell

Run pwd and ls -F to ensure that we’re in the directory we expect.

Two More Shortcuts

The shell interprets the character ~ (tilde) at the start of a path to mean “the current user’s home directory”. For example, if Nelle’s home directory is /homes/nelle, then ~/data is equivalent to /homes/nelle/data. This only works if it is the first character in the path: here/there/~/elsewhere is not here/there/homes/nelle/elsewhere.

Another shortcut is the - (dash) character. cd will translate - into the previous directory I was in, which is faster than having to remember, then type, the full path. This is a very efficient way of moving back and forth between directories. The difference between cd .. and cd - is that the former brings you up, while the latter brings you back. You can think of it as the Last Channel button on a TV remote.

Absolute vs Relative Paths

Starting from /homes/amanda/data, which of the following commands could Amanda use to navigate to her home directory, which is /homes/amanda?

  1. cd .
  2. cd /
  3. cd /homes/amanda
  4. cd ../..
  5. cd ~
  6. cd home
  7. cd ~/data/..
  8. cd
  9. cd ..

Solution

  1. No: . stands for the current directory.
  2. No: / stands for the root directory.
  3. No: Amanda’s home directory is /homes/amanda.
  4. No: this goes up two levels, i.e. ends in /homes.
  5. Yes: ~ stands for the user’s home directory, in this case /homes/amanda.
  6. No: this would navigate into a directory homes in the current directory if it exists.
  7. Yes: unnecessarily complicated, but correct.
  8. Yes: shortcut to go back to the user’s home directory.
  9. Yes: goes up one level.

Key Points

  • The file system is responsible for managing information on the disk.

  • Information is stored in files, which are stored in directories (folders).

  • Directories can also store other directories, which forms a directory tree.

  • cd path changes the current working directory.

  • ls path prints a listing of a specific file or directory; ls on its own lists the current working directory.

  • pwd prints the user’s current working directory.

  • / on its own is the root directory of the whole file system.

  • A relative path specifies a location starting from the current location.

  • An absolute path specifies a location from the root of the file system.

  • Directory names in a path are separated with / on Unix, but \ on Windows.

  • .. means ‘the directory above the current one’; . on its own means ‘the current directory’.