Finding your way around files and directories
Every file on a computer sits somewhere in one tree of directories. The shell always works from one place in that tree, and a path tells it where to go from there, or from the top.
A tree of directories
On a desktop you open folders inside folders until you find the file you want. The terminal works with the same structure, with folders called directories, and together they form a tree. At the very top is the root directory, written as a single /. Every other directory sits somewhere below it.
A path describes where something is in that tree, by listing the directories on the way, separated by slashes. Ava's notes file might live at:
/home/ava/btl-course/notes.txtRead it as: start at the root, go into home, then ava, then btl-course, and there is notes.txt. The directory that belongs to you, /home/ava here, is your home directory. The shell uses ~ as a shortcut for it, which is why the prompt in Terminals and shells showed ~, and ~/btl-course means the btl-course directory inside it.
Ubuntu 24.04 (Windows with WSL, or Linux)
On Ubuntu, home directories live under /home. In WSL, your Windows drives also appear in the tree: the C: drive is at /mnt/c. Keep your course work in your Linux home directory, where file operations are faster, and reach Windows files through /mnt/c only when you need them.
macOS 14 or later with Homebrew
On macOS, home directories live under /Users, so Ava's would be /Users/ava. The folders you see in Finder, such as Documents and Downloads, are directories inside it.
Looking around
The shell always works inside one directory, called the current directory or working directory. pwd, short for "print working directory", tells you which one it is:
$ pwd
/home/avaThe shell also keeps both of these places under names you can print. $HOME holds your home directory and $PWD the current one, so echo $HOME prints /home/ava and echo $PWD prints the same as pwd. Names that start with $ like these get a lesson of their own later; for now, they are two more ways to ask where you are.
ls lists what is inside the current directory, or inside any directory you name. Two options are worth knowing from the start. -l shows one entry per line with details such as size and modification time, and -a also shows hidden entries, whose names start with a dot. Options can be combined:
ls -laIn the long format, a line that starts with d is a directory and a line that starts with - is an ordinary file. You will also see two odd entries at the top: . means "this directory" and .. means "the directory that contains this one", its parent.
To see what a short file says, cat prints its contents into the terminal: cat notes.txt. The next lesson looks at reading files, long and short, more closely.
Moving between directories
cd, short for "change directory", moves the shell somewhere else. You can tell it where to go in two ways.
An absolute path starts with / and is read from the root, so it means the same thing wherever you are. Every Linux and macOS system has a directory called /tmp for temporary files, directly under the root, and this works from anywhere:
cd /tmpA relative path has no leading slash and is read from the current directory. If you are in /home/ava, then cd btl-course takes you to /home/ava/btl-course. Relative paths are shorter, but they depend on where you start, so check with pwd when you are unsure.
A few shortcuts cover most everyday movement:
| Command | Goes to |
|---|---|
cd .. | The directory that contains this one |
cd ~ or just cd | Your home directory |
cd - | The directory you were in before |
The shell turns every path into one place in the tree by the same rule. A path that starts with / starts at the root, one that starts with ~ starts at your home directory, and anything else starts where you are now. Each .. on the way steps up one level. A single misplaced slash is enough to send it somewhere else. If Ava is in /home/ava/btl-course and means its notes directory, cd notes gets her there, but cd /notes looks for notes directly under the root, finds nothing, and answers No such file or directory. The shell stays where it was.
Ava is in /home/ava/btl-course
- notes
- Starts where you are now
- ..
- Steps up one level
- ~/notes
- Starts at your home directory
- /notes
- Starts at the root
Where the shell looks
- notes
- /home/ava/btl-course/notes
- ..
- /home/ava
- ~/notes
- /home/ava/notes
- /notes
- /notes
cd /notes: No such file or directory
She meant the notes directory in btl-course. The leading slash alone changed where the shell started.
Matching names with wildcards
Typing names one by one gets slow when a directory holds dozens of them. The shell can fill them in from a pattern instead. Two characters do most of the work: * matches any run of characters, including none, and ? matches exactly one character. Suppose Ava's notes directory holds jan.txt, feb.txt, march.txt and todo.md:
| Pattern | Matches |
|---|---|
ls *.txt | feb.txt, jan.txt and march.txt |
ls ???.txt | feb.txt and jan.txt, the names with three characters before .txt |
ls *a* | jan.txt and march.txt |
ls */, from Ava's btl-course | Every directory inside btl-course, so ls lists what each one holds |
The shell does the matching before the command starts. ls *.txt becomes ls feb.txt jan.txt march.txt, in alphabetical order, and ls never sees the star. That means the same patterns work with any command that takes names. Like ls without -a, a pattern skips hidden names, so * never matches a name that starts with a dot unless the pattern itself starts with one. When nothing matches, bash leaves the pattern as you typed it, and the command reports that no file by that odd name exists.
Making a place for your work
mkdir makes a new directory. With -p, it also makes any missing directories along the path, and it does not complain if the directory already exists. touch creates an empty file, or updates the modification time of one that exists.
Making a directory and moving into it usually go together, so they are often written on one line joined by &&. It runs the second command only if the first one worked, so a failed mkdir never leaves you running later commands in the wrong place:
mkdir -p ~/btl-course/notes && cd ~/btl-course
touch notes/jan.txt notes/feb.txt
ls -l notesThe opposite of mkdir is rmdir, which removes a directory only when it is empty. If anything is still inside, it refuses with Directory not empty and changes nothing, which makes it a safe way to tidy up directories you made: it can never take files with it.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Build a lab directory and find a hidden code in an archiveMake a directory tree, move around it with absolute and relative paths, match names with wildcards, then unpack an archive and find the code hidden inside it.