Loading
0x10Lesson 2 of 18

Navigate the filesystem

Move around the directory tree with pwd, ls and cd using absolute and relative paths.

20 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • 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 logs

Your 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.txt is the same file.

Three shortcuts appear everywhere:

ShortcutMeans
.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.

explore.sh
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"
Output
labs
logs
notes.txt
---
.
..
.secret
labs
logs
notes.txt
biology
station

A few things to notice:

  • mktemp -d makes a new empty directory with a random name, and cd "$(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 -p creates a whole path at once, including missing parents.
  • Files whose names start with a dot are hidden from a plain ls. Configuration files like ~/.bashrc hide this way.
  • $PWD holds the current directory. We print only its last part with basename, because the sandbox’s full path is random.

Looking closer with ls

ls has dozens of options; these cover most days:

OptionShows
-lthe long format: permissions, owner, size, date
-ahidden files too
-hhuman-readable sizes (4.0K, 12M) with -l
-tnewest first
-Rsubdirectories too, recursively
-1one 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, and cd - goes back.

  • pwd, ls and cd are 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.

Exercise 1

Walk the decks

+25 XP

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:

  1. the contents of station/deck2 (with ls)
  2. the name of the directory you’re in after moving into station/deck2/reactor (with basename "$PWD")
  3. the name of the directory you’re in after moving from there to station/deck1/galley using a relative path
  • Lists and moves around
script.sh
Loading editor…

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.

Exercise 2

Find the hidden file

+25 XP

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
script.sh
Loading editor…

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…

Did you like the lesson? 😆👍
Consider a donation to support our work: