Lesson 2 of 18 · User
Moving around: pwd, ls, cd and paths
Objective
Know at all times where you are in the machine's directories, what is around you, and how to go elsewhere, using absolute or relative paths.
Key points
- Linux stores everything in a single tree that starts at the root
/. There is noC:orD:. pwdshows the current directory,lslists its content,cdchanges directory.- An absolute path starts with
/and works from anywhere:/srv/ecole/notes. - A relative path starts from the current directory:
notes,../notes. .= the current directory,..= the parent directory,~= your home directory.
1. One tree, one root
On Windows, each disk has its own letter. On Linux, everything starts from a single directory called the root, written /. Disks, USB sticks and network shares are "grafted" somewhere into this tree.
/
├── etc/ configuration
├── home/ users' directories
│ ├── karim/
│ └── salma/
├── root/ root's home directory
└── srv/ data served by the machine
└── ecole/
├── classes/
│ ├── 6A/
│ └── 6B/
└── notes/
Note that on Linux, the separator is the forward slash /, not the Windows backslash \.
2. pwd: where am I?
pwd (print working directory) shows the full path of the directory you are in.
pwd # → /home/karim (right after logging in, you are at home)
When you log in, you land in your home directory: /home/karim for Karim, /root for root.
3. ls: what is here?
ls # the content of the current directory
ls /etc # the content of another directory, without going there
ls -l # long format: permissions, owner, size, date
ls -a # EVERYTHING, including hidden files (which start with a dot)
ls -A # like -a, but without the . and .. entries
ls -lh # human-readable sizes: 4,0K, 12M, 1,5G
ls -lt # sorted by date, newest first
ls -ltr # sorted by date, newest LAST (handy for logs)
ls -R # recursive: the directory and all its subdirectories
ls -ld /srv # information about the directory itself, not its content
Let's read a line of ls -l:
drwxr-xr-x 4 root root 4096 Oct 18 10:12 classes
-rw-r--r-- 1 root root 812 Oct 18 10:15 eleves.csv
│└───┬───┘ │ └┬─┘ └┬─┘ └┬─┘ └─────┬────┘ └─ name
│ │ │ │ │ │ └────── date of last modification
│ │ │ │ │ └─────────────── size in bytes
│ │ │ │ └──────────────────── owning group
│ │ │ └───────────────────────── owning user
│ │ └──────────────────────────── number of links
│ └────────────────────────────────── permissions (dedicated lesson later)
└─────────────────────────────────────── type: d = directory, - = file, l = link
Hidden files. Any name that starts with a dot (.bashrc, .ssh) is hidden: ls alone does not show it. This is not a protection, just a way to avoid cluttering the display with settings files.
On most distributions, ls is an alias for ls --color=auto: directories appear in blue, programs in green.
4. cd: changing directory
cd /srv/ecole # go to /srv/ecole (absolute path)
cd classes # go down into the classes subdirectory (relative path)
cd .. # go up one level → /srv/ecole
cd ../.. # go up two levels → /
cd # no argument: back to the home directory
cd ~ # same thing
cd - # back to the previous directory (very handy to switch back and forth)
cd says nothing when everything goes well. If there is an error, it answers for example No such file or directory (the directory does not exist) or Not a directory (you gave a file).
5. Absolute paths and relative paths
This is the key idea of the lesson.
Take a school: the principal is in the office /srv/ecole/classes/6A and wants to reach the grades directory.
| Type | Example | Starts from… | Works… |
|---|---|---|---|
| Absolute | /srv/ecole/notes |
the root / |
from anywhere |
| Relative | ../../notes |
the current directory | only from this directory |
How to read ../../notes from /srv/ecole/classes/6A:
/srv/ecole/classes/6A
.. → /srv/ecole/classes
../.. → /srv/ecole
../../notes → /srv/ecole/notes
The shortcuts to know:
| Symbol | Meaning |
|---|---|
/ at the start |
the root |
. |
the current directory (./script.sh = "the script that is here") |
.. |
the parent directory |
~ |
your home directory (/home/karim) |
~salma |
Salma's home directory (/home/salma) |
When to use which? In a script or a scheduled task, always use absolute paths: the script does not know which directory it will be launched from. At the keyboard, relative paths save time.
6. The Tab key, your best friend
Type cd /sr then Tab: bash completes it to cd /srv/. Type cd /srv/ecole/c then Tab: classes/. If there are several possibilities, a double Tab shows them. This way you avoid typos… and you check along the way that the path exists.
7. A complete example
Mrs Benali, the school secretary, explores the server:
pwd # /root
cd /srv/ecole # she goes to the school directory
ls # classes notes
ls -a # . .. .secret classes notes ← a hidden file!
cd classes/6A # relative path
pwd # /srv/ecole/classes/6A
ls ../../notes # trimestre1.txt ← she looks elsewhere without moving
cd - # back to /srv/ecole
cd # back to her home directory
Common pitfalls
- Forgetting the leading
/:cd srv/ecolelooks for ansrvdirectory in the current directory, not at the root. - Joining
cdand..:cd..does not work in bash; you need a space:cd ... - Thinking a directory is empty because
lsshows nothing: it may contain hidden files; check withls -a. - Spaces in names:
cd Mes documentsfails; writecd "Mes documents"orcd Mes\ documents.
Lab
On this Debian 12 lab, a small school tree has been prepared in /srv/ecole. You will:
- go to the class 6B directory and save its absolute path;
- find the hidden file in
/srv/ecole; - display the detailed information of the directory itself;
- find the relative path that leads from 6A to the grades directory.
Tip: test each cd before writing your answer, and use pwd to check where you ended up.
Quiz
- What does
cddo when typed alone, with no argument? - You are in
/var/log/nginx. Where doescd ../..take you? - Is
/home/karim/facturesan absolute or a relative path? Andfactures/2026? - Which
lsoption shows hidden files? - Why should you prefer absolute paths in a script?
Answers
- It takes you back to your home directory (
~). - To
/var. - The first one is absolute (it starts with
/); the second one is relative. -a(or-A, which does not show.and..).- Because you do not know which directory the script will be launched from: a relative path could point to the wrong place.
Reference sources: Debian Reference and Cahier de l'admin Debian (free licences), Ubuntu Server Guide (CC BY-SA 3.0). Original MasterLinux text.