Navigating the filesystem
Learn pwd, ls, and cd so you always know where you are and can move anywhere in the filesystem with confidence.
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 last lesson left you staring at that ~ in the prompt, having realised the shell is always standing somewhere in the filesystem. This lesson makes “somewhere” precise and movable. Three commands do the whole job — pwd to ask where you are, ls to see what is around you, cd to move — held together by one idea, absolute versus relative paths, that quietly causes most “file not found” errors. Start with the shape of the place you are moving through.
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 /.
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
| Flag | What it does |
|---|---|
-l | Long format — shows permissions, size, owner, date |
-a | All files — includes hidden files that start with . |
-h | Human-readable sizes (KB, MB) — combine with -l |
-t | Sort 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.
| Token | Meaning |
|---|---|
. | 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
In one breath
The filesystem is a tree rooted at /, and the shell always sits at one spot in it — your working directory. Three commands run your whole sense of place: pwd prints where you are, ls lists what is there (-l long, -a hidden, -h readable sizes, -t by time), and cd moves you. Five tokens are shorthand you will type constantly — . here, .. parent, ~ home, / root, - previous directory. The idea that prevents most “file not found” pain: an absolute path starts with / and works from anywhere, while a relative path is resolved from wherever you currently stand — so the same cd data succeeds or fails depending on your location. Lean on Tab completion, and prefer absolute paths in scripts.
Practice
Before the quiz, trace your position. You start at /home/alex/project and run, in order: cd .., then cd .., then pwd. What does that final pwd print? Now, from that spot, which single cd command jumps you straight back to /home/alex/project, is it absolute or relative, and why would a bare cd project fail from where you now stand?
Quick check
A question to carry forward
Notice that everything in this lesson was read-only. pwd, ls, cd — you looked at the tree and walked around it, but you never once changed it. A cursor that can only sightsee is not much use. The actual work of a terminal is editing that tree: conjuring a new folder into existence, creating an empty file, copying something to a backup, renaming a mess of files, and — carefully — deleting what you no longer need. Each of those is a single short command, and one of them has no undo and no “are you sure?” at all. So how do you reshape the filesystem instead of just touring it? That is the next lesson.