2  Navigating the file system

Note⏳ Time
  • Teaching: 15 min
  • Exercises: 10 min
NoteπŸ€” Questions
  • How is the file system organised?
  • How do I know where I am?
  • How do I move between directories?
Note🎯 Objectives
  • Explain the hierarchical structure of the Unix file system
  • Navigate using pwd, ls, and cd
  • Distinguish between absolute and relative paths
  • Use tab completion to avoid typing mistakes

2.1 The file system

Files and directories on a Unix system are arranged in a hierarchy, like an upside-down tree. The base of the tree is called the root directory and is written as /. Every file and directory on the system lives somewhere beneath it.

/
β”œβ”€β”€ home/
β”‚   └── username/
β”‚       β”œβ”€β”€ Documents/
β”‚       └── Downloads/
β”œβ”€β”€ usr/
└── tmp/

When you open a terminal, you start in your home directory typically /home/username on Linux or /Users/username on a Mac. This is your personal space on the system.


2.3 Listing (What is there?)

The command ls (list) shows the contents of your current directory:

ls

On its own, ls gives you a simple list of names. Flags make it more informative:

Command What it shows
ls -F Adds / after directories, * after executables
ls -l Long format: permissions, size, date
ls -a All files, including hidden ones (starting with .)
ls -lh Long format with human-readable file sizes
Tip

Flags can be combined: ls -lF and ls -lah are both valid. The order of flags generally does not matter.


2.4 Making a directory (Creating a place to work)

The command mkdir (make directory) creates a new directory:

mkdir training

Check it was created:

ls
Note

Directory names should not contain spaces. Use underscores (_) or hyphens (-) instead for example, my_data or shell-intro. Spaces in filenames cause all sorts of problems on the command line.


2.5 Changing directory (Going somewhere)

The command cd (change directory) moves you into a different directory. Let’s go into the one we just created:

cd training

Confirm you have moved:

pwd

Going up (Back to the parent)

Two dots (..) always refer to the directory above your current location (the parent directory):

cd ..

Going home

A tilde (~) always refers to your home directory. From anywhere on the system:

cd ~

or simply:

cd

Both take you straight home.


2.6 Absolute vs. relative paths

A path is the address of a file or directory in the file system.

  • An absolute path starts from the root (/) and works from anywhere on the system:

    cd /home/username/training
  • A relative path is given relative to where you currently are:

    cd training
Tip

Use absolute paths in scripts they always work regardless of where the script is called from. Use relative paths for quick interactive navigation.


2.7 Tab completion (Let the shell do the typing)

Typing long directory or file names is error-prone. Type the first few characters and press Tab the shell will autocomplete:

cd trai<Tab>

becomes:

cd training

If there are multiple matches, press Tab twice to see all options.

Tip

Tab completion is one of the most valuable habits you can build at the command line. Use it constantly it saves time and prevents typos.


2.8 Reading the manual (How does this work?)

Every command comes with a built-in manual. The command man (manual) opens it:

man ls

Use the arrow keys or Space to scroll. Press q to quit.

Tip

No one memorises all the options for every command. The manual is always there. For a quicker summary, many commands also accept --help:

ls --help

Caution✏️ Exercise 2.1
  1. Navigate into your training directory
  2. Use print working directory to confirm where you are
  3. Go back to your home directory using ..
  4. Go back to training using the ~ notation
cd training
pwd
cd ..
cd ~/training
Caution✏️ Exercise 2.2

Inside training, create two new directories called data and results in a single command:

mkdir data results

List the contents with ls -F. What does the trailing / tell you?

ls -F

The trailing / indicates that data and results are directories, not files. Files have no trailing character; executable files get a *.

ImportantπŸš€ Bonus: for those who want more

The command tree prints a visual representation of a directory structure:

tree ~/training

If tree is not installed: sudo apt install tree (Ubuntu), brew install tree (Mac with Homebrew), or it may already be available in MobaXterm.

Also try: what happens when you run cd with no argument at all? Where do you end up?


TipπŸ”‘ Key points
  • The Unix file system is a hierarchy rooted at /
  • pwd tells you where you are; ls shows what is there; cd moves you around; mkdir creates directories
  • .. refers to the parent directory; ~ refers to your home directory
  • Tab completion reduces errors and saves time use it constantly
  • man opens the manual for any command