Book 2 — The Magic World of the Terminal

Python for All

Chapter Two — Navigating Your File System

Thanasis Troboukis  ·  All Books

Book Two · Chapter Two

Navigating Your File System

Your computer is a tree of folders and files. This chapter teaches you to move through it with confidence using ls and cd.

Your File System Is a Tree

Every file on your computer lives at a specific address — its path. To understand paths, you need to understand how the file system is organised. Think of it as an upside-down tree: a single root at the top, with branches (directories) and leaves (files) growing downward.

On macOS and Linux, the root of the entire tree is written as / (a single forward slash). Everything on your computer lives inside it. Your user folder sits at /Users/yourname, and everything you create goes inside that.

Here is what a typical student workspace looks like as a tree:

/Users/student/
├── Desktop/
├── Downloads/
│   └── budget_report.pdf
└── Files/
    └── data_journalism/
        ├── notes.txt
        ├── cities.csv
        └── sources/
            └── contacts.txt

Reading this tree: student is a directory inside Users. Inside student there are three directories: Desktop, Downloads, and Files. Inside Files is data_journalism, which contains two files and one sub-directory called sources.

The full path to contacts.txt is /Users/student/Files/data_journalism/sources/contacts.txt. Each / in the path is a branch — a step down in the tree.

Home directory: your home directory is the folder with your username — /Users/student on the Mac, /home/student on Linux. The terminal uses ~ as a shorthand for it. Whenever you open a new terminal window, you start in your home directory.

ls — Listing What Is Around You

ls stands for list. It is one of the two commands you will use most — the other being cd. Together they let you explore the entire file system without ever touching a mouse.

On its own, ls prints the names of files and directories in your current location. Add flags for more detail:

Command What it shows
lsNames only — fast overview
ls -lLong format: permissions, owner, size in bytes, date, name
ls -lahLong format + all files (including hidden) + human-readable sizes (KB, MB)
ls DownloadsList a specific directory without moving into it

In the long format output (ls -l), the first character of each line tells you the entry type: d means directory, - means file. The number at the far left of the size column is the size in bytes (or kilobytes/megabytes with the -h flag).

Terminal · ls Lab

          

$

Try: ls · ls -l · ls -lah · ls Downloads · ls Files/data_journalism

cd — Changing Directory

cd stands for change directory. It moves you from one location to another in the file system. After running cd, your prompt will reflect the new location — and all subsequent commands will operate relative to it.

Command What it does
cd DownloadsGo into the Downloads directory
cd Files/data_journalismGo two levels down in one step
cd ..Go up one level (to the parent directory)
cd ../..Go up two levels
cd ~Go directly to your home directory from anywhere
cdSame as cd ~ — go home

The two dots .. are a special name that always means "the parent directory" — the directory one level up from where you are. Similarly, a single dot . means "the current directory." These two shorthands appear everywhere in terminal work.

Terminal · cd Lab

          

$

Try: cd Downloads → pwd → ls → cd .. → cd Files/data_journalism → pwd → cd ~ → pwd

Common mistake: running cd data_journalism when you are not in the parent directory will give you an error. Always check where you are with pwd before navigating. If you get lost, cd ~ always takes you home.

Absolute and Relative Paths

Any file or directory can be addressed in two ways: by its absolute path or by a relative path. Understanding the difference is the key to never getting lost.

An absolute path starts with / and gives the full address from the root of the drive. It works the same way regardless of where you currently are. Example:

cd /Users/student/Files/data_journalism

This takes you to data_journalism from anywhere in the file system — even if you are currently on the Desktop.

A relative path does not start with /. It describes a location relative to where you currently are. If you are already in /Users/student, then:

cd Files/data_journalism

…takes you to the same destination, because the terminal looks for Files inside your current directory.

Notation Meaning
/Root of the entire drive — start of every absolute path
~Your home directory (/Users/student)
.The current directory
..The parent directory (one level up)
When to use each: use relative paths for short moves within a project folder. Use absolute paths in scripts and when you need to refer to a file from any location — for example, when running a Python script from a different directory.

Practice — Navigate a Realistic File System

The sandbox below has a more complete file system to explore. Your challenge: reach specific files using a mix of relative and absolute paths, confirm your location with pwd, and list directory contents with ls.

Suggested sequence to try:

  1. Start at home (pwd to confirm).
  2. Navigate to Downloads using a relative path. Run ls.
  3. Go back home with cd ~.
  4. Navigate to Files/data_journalism/sources in one command.
  5. Confirm your location with pwd.
  6. Go up two levels with cd ../... Where are you now?
  7. Navigate to /Users/student/Desktop using an absolute path.
Terminal · Navigation Challenge

          

$

Use pwd to check location, ls to look around, cd to move

What you have learned in Chapter 2: the file system is a tree rooted at /; ls and ls -lah list what is in a directory; cd moves you between directories; .. means parent and ~ means home; absolute paths start with / and relative paths start from where you are. In Chapter 3 you will learn to create, move, copy, and delete files.

Chapter Navigation

Move between chapters.