Module 1 · First steps in the terminal
Absolute and relative paths, . and ..
The one rule that makes every path make sense: a leading slash means start at the root, anything else means start here. Plus the two dot entries every directory contains.
What you will learn
- Tell an absolute path from a relative one at a glance.
- Use `.` and `..` inside paths, not only with `cd`.
- Resolve a relative path by hand and with `realpath`.
A path is a set of directions to a file. There are two kinds, and the difference is the first character. An absolute path starts with /: directions from the root of the tree, valid from anywhere, like a full postal address. A relative path starts with anything else: directions from the directory you are standing in, like "second door on the left". The same relative path points at different things from different places; an absolute path never changes meaning.
~% cd /root/lab/l6
l6% ls a/b (relative: /root/lab/l6/a/b)
note.txt
l6% ls /root/lab/l6/a/b (absolute: same place)
note.txt
l6% cd a/b
b% ls a/b (relative again, from a new place)
ls: a/b: No such file or directory
The two entries every directory has
Every directory contains two special names. . (one dot) is the directory itself. .. (two dots) is its parent. ls -a shows them at the top of every listing. They are real entries, not shell tricks, which is why they work inside any path for any command: cp ../config.txt . copies a file from the parent into here; ls ../../etc lists a directory two levels up and over; ./run.sh means "the run.sh in this very directory".
| You are in | You type | It means |
|---|---|---|
/root/lab/l6/a/b | .. | /root/lab/l6/a |
/root/lab/l6/a/b | ../../c | /root/lab/l6/c |
/root/lab/l6/a/b | ./note.txt | /root/lab/l6/a/b/note.txt |
/root/lab/l6/a/b | /etc/hosts | /etc/hosts — absolute, location irrelevant |
/ | .. | / — the root is its own parent |
Resolving a path by hand
To work out where a relative path leads, start from your working directory and apply each component in turn: a name enters it, .. goes up, . stays. From /root/lab/l6/a/b, the path ../../c/x.txt goes up to a, up to l6, into c, to x.txt: /root/lab/l6/c/x.txt. When you would rather not do the arithmetic, realpath does it for you and prints the absolute result: realpath ../../c prints /root/lab/l6/c. It also resolves symbolic links, which pwd and cd do not always do.
b% pwd
/root/lab/l6/a/b
b% realpath ../../c
/root/lab/l6/c
b% realpath .
/root/lab/l6/a/b
b% touch ../../c/hop.txt
b% ls /root/lab/l6/c
hop.txt
Which kind to use
Relative paths are shorter and move with you: a project you copy elsewhere keeps working if its internal references are relative. Absolute paths are unambiguous and safe in scripts, scheduled jobs and anything that might run from an unknown directory. A fair rule: type relative paths while exploring interactively, write absolute paths — or paths relative to a known anchor — into anything you save. The home shortcut ~ behaves like an absolute path: ~/lab is /root/lab from anywhere.
Commands in this lesson
| Command | What it does |
|---|---|
/absolute/path | Starts with `/`: counted from the root, same from anywhere. |
relative/path | No leading `/`: counted from the working directory. |
. | This directory. |
.. | The parent directory; `../..` is the grandparent. |
realpath PATH | Print PATH as an absolute path, links resolved. |
~ | Your home directory, `/root` here. |
Quiz
Which of these is an absolute path?
- `lab/l6`
- `../etc`
- `/root/lab`
- `./run.sh`
You are in `/root/lab/l6/a/b`. Where does `../../c` point?
- `/root/lab/l6/c`
- `/root/lab/l6/a/c`
- `/root/lab/c`
What is `.` in a path?
- The current directory
- A hidden file
- The root directory
What does `realpath ..` print when you are in `/usr/bin`?
- `..`
- `/usr`
- `/usr/bin/..`
Where does `cd ..` take you from `/`?
- It fails
- It stays in `/`; the root is its own parent
- To your home directory
Practice
Go to /root/lab/l6/a/b. From there, using a relative path that climbs two levels, create an empty file `hop.txt` inside /root/lab/l6/c.
While standing in /root/lab/l6/a/b, print the absolute path of the parent directory without leaving.
From any directory, create the empty file /root/lab/l6/c/abs.txt using its absolute path.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.