Linux Command-Line Fundamentals
Exit

Lesson 2 of 18 · User

Moving around: pwd, ls, cd and paths

25 min Debian 12

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 no C: or D:.
  • pwd shows the current directory, ls lists its content, cd changes 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/ecole looks for an srv directory in the current directory, not at the root.
  • Joining cd and ..: cd.. does not work in bash; you need a space: cd ...
  • Thinking a directory is empty because ls shows nothing: it may contain hidden files; check with ls -a.
  • Spaces in names: cd Mes documents fails; write cd "Mes documents" or cd Mes\ documents.

Lab

On this Debian 12 lab, a small school tree has been prepared in /srv/ecole. You will:

  1. go to the class 6B directory and save its absolute path;
  2. find the hidden file in /srv/ecole;
  3. display the detailed information of the directory itself;
  4. 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

  1. What does cd do when typed alone, with no argument?
  2. You are in /var/log/nginx. Where does cd ../.. take you?
  3. Is /home/karim/factures an absolute or a relative path? And factures/2026?
  4. Which ls option shows hidden files?
  5. Why should you prefer absolute paths in a script?
Answers
  1. It takes you back to your home directory (~).
  2. To /var.
  3. The first one is absolute (it starts with /); the second one is relative.
  4. -a (or -A, which does not show . and ..).
  5. 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.

Practical exercises

To do on Debian 12

The online terminal is coming soon. Meanwhile, do these tasks on your own Linux machine, a virtual machine or WSL.

  1. Go to the directory of class 6B, then save the absolute path of the current directory in /root/chemin.txt.
    Show a hintcd /srv/ecole/classes/6B, then pwd > /root/chemin.txt
    Show the solution
    cd /srv/ecole/classes/6B && pwd > /root/chemin.txt

    Check : grep -qx /srv/ecole/classes/6B /root/chemin.txt

  2. The /srv/ecole directory contains a hidden file. Save the COMPLETE listing of this directory (hidden files included) in /root/liste.txt.
    Show a hintHidden files start with a dot. The -a option of ls shows them: ls -a /srv/ecole > /root/liste.txt
    Show the solution
    ls -a /srv/ecole > /root/liste.txt

    Check : grep -qx .secret /root/liste.txt

  3. Save in /root/details.txt the detailed information of the /srv/ecole directory ITSELF (not its content).
    Show a hintLong format (-l) + the -d option to describe the directory itself: ls -ld /srv/ecole
    Show the solution
    ls -ld /srv/ecole > /root/details.txt

    Check : grep -q '^d.* /srv/ecole$' /root/details.txt

  4. You are in /srv/ecole/classes/6A. Which RELATIVE path leads to the /srv/ecole/notes directory? Write it (and only it) in /root/relatif.txt.
    Show a hintEach .. goes up one level: 6A → classes → ecole, then you go down into notes. Test it with cd before writing it with echo.
    Show the solution
    echo ../../notes > /root/relatif.txt

    Check : grep -qx '../../notes' /root/relatif.txt

Official references consulted: Debian Reference — chapitre 1, système de fichiers et commandes de base ; Cahier de l'admin Debian — l'interpréteur de commandes. Original MasterLinux text.