Navigate the filesystem
Move around the directory tree with pwd, ls and cd using absolute and relative paths.
- Describe the Unix directory tree, starting from /
- Tell absolute paths from relative ones and use ., .. and ~
- Move around and look around with cd, pwd and ls
On Linux and macOS, every file lives in one tree of directories (folders) that starts at the root, written /. There are no drive letters: disks and USB sticks are attached somewhere inside the tree.
1/
2├── bin/ essential programs (ls, cat, ...)
3├── etc/ system configuration files
4├── home/
5│ └── ada/ your home directory, also written ~
6│ └── station/
7│ ├── logs/
8│ └── notes.txt
9├── tmp/ temporary files, cleared regularly
10└── var/log/ system logsYour shell always has a current working directory - the place you are “standing”. pwd (print working directory) shows it, ls lists what’s there, and cd (change directory) moves you.
Absolute and relative paths
A path names a file or directory:
- An absolute path starts with
/and is spelled out from the root:/home/ada/station/notes.txt. It means the same thing wherever you are. - A relative path doesn’t start with
/, so it’s followed from the current directory: from/home/ada,station/notes.txtis the same file.
Three shortcuts appear everywhere:
| Shortcut | Means |
|---|---|
. | the current directory |
.. | the parent directory (one level up) |
~ | your home directory (/home/ada) |
And cd has a few tricks: cd alone goes home, cd - jumps back to the previous directory, and cd .. climbs one level.
1cd "$(mktemp -d)" # start in a fresh, empty sandbox directory
2mkdir -p station/logs station/labs/biology
3touch station/notes.txt station/.secret
4
5cd station
6ls # hidden files (starting with .) are skipped
7echo "---"
8ls -a # -a shows everything, including . and ..
9cd labs/biology
10basename "$PWD" # the last part of the current path
11cd ../..
12basename "$PWD"labs logs notes.txt --- . .. .secret labs logs notes.txt biology station
A few things to notice:
mktemp -dmakes a new empty directory with a random name, andcd "$(mktemp -d)"moves into it. Exercises use this so every run starts clean. (The$(...)part runs a command and uses its output - you’ll learn it properly later.)mkdir -pcreates a whole path at once, including missing parents.- Files whose names start with a dot are hidden from a plain
ls. Configuration files like~/.bashrchide this way. $PWDholds the current directory. We print only its last part withbasename, because the sandbox’s full path is random.
Looking closer with ls
ls has dozens of options; these cover most days:
| Option | Shows |
|---|---|
-l | the long format: permissions, owner, size, date |
-a | hidden files too |
-h | human-readable sizes (4.0K, 12M) with -l |
-t | newest first |
-R | subdirectories too, recursively |
-1 | one name per line |
Options combine: ls -lah is the classic “show me everything here”.
Key takeaways
Everything lives in one tree starting at
/; your shell always has a current directory.Absolute paths start with
/; relative paths are followed from where you are..is here,..is the parent,~is home, andcd -goes back.pwd,lsandcdare how you look and move; quote names that contain spaces.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Bash scripts
Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.
Walk the decks
The starter code builds this tree in a fresh directory:
1station/
2├── deck1/
3│ └── galley/
4└── deck2/
5 ├── lab/
6 └── reactor/Add commands so the script prints, in order:
- the contents of
station/deck2(withls) - the name of the directory you’re in after moving into
station/deck2/reactor(withbasename "$PWD") - the name of the directory you’re in after moving from there to
station/deck1/galleyusing a relative path
- Lists and moves around
Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.
Find the hidden file
The starter code hides a file somewhere in vault. Print a listing of vault that includes hidden files but not . and .., then print the contents of the hidden file with cat. Look up which ls option does that with ls --help (hint: it’s the capital-letter cousin of -a).
- Reveals the hidden file
Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…