2 Navigating the file system
2.1 The file system
Files and directories on a Unix system are arranged in a hierarchy, like an upside-down tree. The base of the tree is called the root directory and is written as /. Every file and directory on the system lives somewhere beneath it.
/
βββ home/
β βββ username/
β βββ Documents/
β βββ Downloads/
βββ usr/
βββ tmp/
When you open a terminal, you start in your home directory typically /home/username on Linux or /Users/username on a Mac. This is your personal space on the system.
2.2 Print working directory (Where am I?)
At any moment, you are working inside one directory your current working directory. The command pwd (print working directory) tells you exactly where that is:
pwdYour output will look something like:
/home/username
or on a Mac:
/Users/username
2.3 Listing (What is there?)
The command ls (list) shows the contents of your current directory:
lsOn its own, ls gives you a simple list of names. Flags make it more informative:
| Command | What it shows |
|---|---|
ls -F |
Adds / after directories, * after executables |
ls -l |
Long format: permissions, size, date |
ls -a |
All files, including hidden ones (starting with .) |
ls -lh |
Long format with human-readable file sizes |
Flags can be combined: ls -lF and ls -lah are both valid. The order of flags generally does not matter.
2.4 Making a directory (Creating a place to work)
The command mkdir (make directory) creates a new directory:
mkdir trainingCheck it was created:
lsDirectory names should not contain spaces. Use underscores (_) or hyphens (-) instead for example, my_data or shell-intro. Spaces in filenames cause all sorts of problems on the command line.
2.5 Changing directory (Going somewhere)
The command cd (change directory) moves you into a different directory. Letβs go into the one we just created:
cd trainingConfirm you have moved:
pwdGoing up (Back to the parent)
Two dots (..) always refer to the directory above your current location (the parent directory):
cd ..Going home
A tilde (~) always refers to your home directory. From anywhere on the system:
cd ~or simply:
cdBoth take you straight home.
2.6 Absolute vs. relative paths
A path is the address of a file or directory in the file system.
An absolute path starts from the root (
/) and works from anywhere on the system:cd /home/username/trainingA relative path is given relative to where you currently are:
cd training
Use absolute paths in scripts they always work regardless of where the script is called from. Use relative paths for quick interactive navigation.
2.7 Tab completion (Let the shell do the typing)
Typing long directory or file names is error-prone. Type the first few characters and press Tab the shell will autocomplete:
cd trai<Tab>becomes:
cd trainingIf there are multiple matches, press Tab twice to see all options.
Tab completion is one of the most valuable habits you can build at the command line. Use it constantly it saves time and prevents typos.
2.8 Reading the manual (How does this work?)
Every command comes with a built-in manual. The command man (manual) opens it:
man lsUse the arrow keys or Space to scroll. Press q to quit.
No one memorises all the options for every command. The manual is always there. For a quicker summary, many commands also accept --help:
ls --help