datarekha

Navigating the filesystem

Learn pwd, ls, and cd so you always know where you are and can move anywhere in the filesystem with confidence.

7 min read Beginner Command Line Lesson 2 of 14

What you'll learn

  • pwd, ls, and cd: the three commands you will use in every terminal session
  • Absolute vs relative paths: why the distinction is the root cause of most file-not-found errors
  • Special shortcuts: . (here), .. (parent), ~ (home), / (root), - (previous dir)

Before you start

The filesystem is a tree

Your operating system stores files in a hierarchy of folders called directories. Picture it as an upside-down tree: a single root at the top, branches spreading downward into more directories, leaves at the tips being files.

On macOS and Linux the root is always written as / — a single forward slash. Everything on your machine lives somewhere inside /.

//home/etc/usr/home/alexDownloadsproject

Every path traces from root (/) downward. /home/alex/project is three levels deep.

On macOS the layout differs slightly — your home directory is /Users/yourname instead of /home/yourname — but the tree concept is identical.


pwd — print working directory

When you open a terminal, the shell puts you somewhere in the tree. That location is called your working directory (also called the current directory). Think of it as the folder that is “open” right now.

pwd tells you exactly where you are:

pwd
/home/alex/project

Read that output from left to right: start at root /, go into home, then into alex, then into project. That full address is called an absolute path — more on that in a moment.


ls — list directory contents

ls shows what is inside the working directory.

ls
README.md  data  notebooks  src

Plain ls is fine for a quick look, but the flags are where it gets useful.

Useful ls flags

FlagWhat it does
-lLong format — shows permissions, size, owner, date
-aAll files — includes hidden files that start with .
-hHuman-readable sizes (KB, MB) — combine with -l
-tSort by modification time, newest first

You can combine flags. -lh and -lha are both common:

ls -lh
total 24K
drwxr-xr-x  3 alex  staff   96B Jun  3 09:12 data
drwxr-xr-x  4 alex  staff  128B Jun  4 14:30 notebooks
-rw-r--r--  1 alex  staff  1.2K Jun  4 14:31 README.md
drwxr-xr-x  5 alex  staff  160B May 28 11:00 src

The leading d means directory; - means regular file. The size column reads cleanly in human units thanks to -h.

To list a specific directory without going there, pass its path as an argument:

ls -l /etc

cd — change directory

cd moves your working directory. After cd, pwd will show a new location.

cd notebooks
pwd
/home/alex/project/notebooks

The special paths

The shell recognises five special path tokens you will type constantly.

TokenMeaning
.The current directory (where you already are)
..The parent directory (one level up)
~Your home directory
/The filesystem root
-The previous working directory (toggle)

Go up one level:

cd ..
pwd
/home/alex/project

Go up two levels at once:

cd ../..
pwd
/home/alex

Jump home from anywhere:

cd ~
pwd
/home/alex

Toggle back to where you just were — handy when you jump deep into one directory and then want to return:

cd /home/alex/project/data
cd ~
cd -
/home/alex/project/data

Absolute vs relative paths

This is the single most important idea in the lesson.

An absolute path starts with /. It is the full address from root down to the target, and it works regardless of where you currently are:

cd /home/alex/project/data

A relative path does not start with /. It is interpreted as starting from your current working directory:

cd data

That second command only works if a directory named data exists inside your current working directory. If you have already moved somewhere else, the shell will report an error because data no longer means the same thing.

Rule of thumb: when you are writing a script or sharing a path with a colleague, prefer absolute paths. When you are typing interactively and you are sure of your location, relative paths are faster.


Tab completion — navigate faster

Typing long paths by hand invites typos. Press Tab after a partial name and the shell completes it for you:

cd not[Tab]

If notebooks is the only match, the shell fills in the rest immediately. If there are multiple matches, press Tab a second time to see the list.

Tab completion works with cd, ls, command names, and most other arguments. Making it a habit cuts navigation time significantly.


Quick reference

pwd                     # where am I?
ls                      # what is here?
ls -lh                  # detailed list with readable sizes
ls -a                   # include hidden files
cd foldername           # go into foldername (relative)
cd /absolute/path       # go to an absolute path
cd ..                   # go up one level
cd ~                    # go home
cd -                    # toggle to previous directory

Quick check

0/3
Q1You are in /home/alex and you run `cd project/data`. Which best describes `project/data`?
Q2What does `cd -` do?
Q3A teammate gives you a script that contains `cd reports && ls`. You copy the script and run it from /home/alex/project, but the script fails. The most likely cause is:

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Cheat sheets

Related lessons

Skip to content