Absolute and relative paths
On this page
You are in ~/lab/Documents, reading your notes, and you want to check your to-do list as well. todo.txt is one level up, in ~/lab. With the commands from lesson B05.1, that takes three steps: go up, read, and come back down.
learner@rootvolt:~/lab/Documents$ cat notes.txtBuy printer paperRenew lab VM snapshotlearner@rootvolt:~/lab/Documents$ cd ..learner@rootvolt:~/lab$ cat todo.txtInstall UbuntuVerify the ISO checksumTake a snapshotlearner@rootvolt:~/lab$ cd DocumentsIt works, but you moved twice to read one file. And the further away a directory is, the more steps you need: from /, reaching ~/lab/projects takes four cd commands, one for each level.
By the end of this lesson, you can name any file in ~/lab in two ways, say which file a name points to from a given directory, and reach any directory with one cd.
Before you start
Section titled “Before you start”- Root, home, and the current directory (B05.1):
/,~,.,..,pwd, andcd. - The
~/labpractice directory from lesson B04.1 (setup).
Open a terminal and run cd ~/lab, then cd Documents. Your prompt ends in ~/lab/Documents$.
Name a file from the top
Section titled “Name a file from the top”You already know one name for this directory that starts at the top. Ask for it:
pwd/home/learner/lab/DocumentsRead it from the left, as in lesson B05.1: /, then home, learner, lab, and Documents. todo.txt is in lab, so its full name is /home/learner/lab/todo.txt. Give that name to cat, without moving:
cat /home/learner/lab/todo.txtInstall UbuntuVerify the ISO checksumTake a snapshotThe prompt still ends in ~/lab/Documents$: you did not move. Now go to the top, and run the same command again:
cd /cat /home/learner/lab/todo.txtInstall UbuntuVerify the ISO checksumTake a snapshotThe same name works from / too. A name for a file or directory, with / between the directory names on the way, is a path. A path that starts with / is an absolute path. It starts at the root directory and names every directory down to the target, so it means the same file from anywhere. pwd always prints an absolute path.
ls takes an absolute path as well. From /, list the lab:
ls /home/learner/labDocuments projects todo.txtName a file from where you are
Section titled “Name a file from where you are”Go back with cd ~/lab. In module B04, you ran commands like this one there:
cat Documents/notes.txtBuy printer paperRenew lab VM snapshotDocuments/notes.txt is a path too, but it does not start with /, so it is read from the current directory, ~/lab: there is Documents, and inside it notes.txt. A path that does not start with / is a relative path. A name with no / at all, such as todo.txt, is the shortest relative path.
Now move into projects and run the same command, unchanged. Guess first: what does cat print this time?
cd projectscat Documents/notes.txtRun it, then compare with your guess
cat: Documents/notes.txt: No such file or directoryThe same words, but a different place to start. From ~/lab/projects, Documents/notes.txt means ~/lab/projects/Documents/notes.txt, and projects is empty.
A relative path depends on where you are. From another directory, the same relative path can point to a different file, or to nothing.
Go up inside a path with ..
Section titled “Go up inside a path with ..”You are in projects, and you still want notes.txt. .., the parent directory from lesson B05.1, also works inside a path:
cat ../Documents/notes.txtBuy printer paperRenew lab VM snapshotRead a relative path from the left, one name at a time, starting in the current directory:
| Name in the path | Where it leads |
|---|---|
| (start) | ~/lab/projects, the current directory |
.. |
Its parent, ~/lab |
Documents |
~/lab/Documents |
notes.txt |
The file ~/lab/Documents/notes.txt |
The prompt shows that you are still in projects: cat followed the path, but your shell did not move.
The diagram shows both names for this file.
In words: the absolute path /home/learner/lab/Documents/notes.txt starts at / and goes down through home, learner, lab, and Documents. The relative path ../Documents/notes.txt starts at ~/lab/projects, goes up to lab with .., then down through Documents. Both end at the same file. Only the directories on the two routes are drawn.
In lesson B02.2, from inside the altered folder, ../SHA256SUMS named the file SHA256SUMS one level up in the same way. And .. solves the opening in one command, from ~/lab/Documents:
learner@rootvolt:~/lab/Documents$ cat ../todo.txtInstall UbuntuVerify the ISO checksumTake a snapshot./ means here
Section titled “./ means here”A path can also contain ., the current directory itself (lesson B05.1). Some guides write ./ in front of a name. Go up to ~/lab and try it:
cd ..cat ./todo.txtInstall UbuntuVerify the ISO checksumTake a snapshot./todo.txt means “todo.txt in this directory”, the same file as todo.txt. For cat, the ./ changes nothing.
One file, two names
Section titled “One file, two names”The replay below reads todo.txt twice: from ~/lab with a relative path, and from / with an absolute path. Watch the cwd marker move with cd /, and the argument mark land on the same file both times.
learner@rootvolt:~/lab$ cat todo.txtInstall Ubuntu
Verify the ISO checksum
Take a snapshotlearner@rootvolt:~/lab$ cd /learner@rootvolt:/$ cat /home/learner/lab/todo.txtInstall Ubuntu
Verify the ISO checksum
Take a snapshotlearner@rootvolt:/$ Files Other folders in / are not shown
- /cwd
- home/
- learner/~
- lab/
- Documents/
- notes.txt-rw-r--r--
- report.txt-rw-r--r--
- .draft.txthidden-rw-r--r--
- projects/
- todo.txtargument-rw-r--r--
Steps (9)
- Type
cat todo.txt— Relative: read from the current directory, ~/lab - Press Enter
- Output:
Install Ubuntu Verify the ISO checksum Take a snapshot - Type
cd /— Move to the root directory - Press Enter
- Type
cat /home/learner/lab/todo.txt— Absolute: the whole route from / - Press Enter
- Output:
Install Ubuntu Verify the ISO checksum Take a snapshot - Two working directories, two names, one file: ~/lab/todo.txt.
In words: in ~/lab, cat todo.txt prints the three lines of todo.txt. cd / moves the marker to /. There, cat /home/learner/lab/todo.txt prints the same three lines. Both times, the argument mark is on todo.txt.
Where ~ fits
Section titled “Where ~ fits”~/lab/todo.txt does not start with /, but it is not a relative path either. Lesson B05.1 said that Bash replaces ~ at the start of a word with your home directory before the command runs. echo prints the words it is given (lesson B02.4), so it shows what a program receives:
echo ~/lab/todo.txt/home/learner/lab/todo.txtThe program never sees the ~. It receives the absolute path /home/learner/lab/todo.txt, so a path that starts with ~/ works from any directory, as an absolute path does. From /:
learner@rootvolt:/$ cat ~/lab/todo.txtInstall UbuntuVerify the ISO checksumTake a snapshotSo ~/… is not a third kind of path. It is a short way to type an absolute path that starts in your home directory.
Reach any directory with one cd
Section titled “Reach any directory with one cd”cd takes a path like any other command, absolute or relative. The four cd commands from the opening become one:
cd /cd /home/learner/lab/projectspwd/home/learner/lab/projectsFrom projects, a relative path with .. reaches Documents, which has the same parent:
cd ../Documentspwd/home/learner/lab/DocumentsAnd cd ~/lab brings you back to the lab from anywhere:
cd ~/labpwd/home/learner/labA relative path is shorter when the target is close. An absolute path, or one that starts with ~/, does not depend on where you are, so use it when the target is far away or when you are not sure where you are.
Mistakes to recognize
Section titled “Mistakes to recognize”Read each message in three parts, as in lesson B04.4: who is speaking, which word it names, and what went wrong.
A / at the start of a relative path.
learner@rootvolt:~/lab$ cat /Documents/notes.txtcat: /Documents/notes.txt: No such file or directorycat is speaking, so the command name is fine. The word is the path /Documents/notes.txt, and it does not exist. The / at the start made it an absolute path: it starts at the root directory, and / has no directory called Documents, as the ls / output in lesson B05.1 shows. Remove the first /: cat Documents/notes.txt.
A directory you are already in.
learner@rootvolt:~/lab$ cd labbash: cd: lab: No such file or directoryBash speaks for cd. The word is lab, a relative path, so cd looks for lab inside the current directory and finds no ~/lab/lab. Read the prompt: you are already in ~/lab. A guide that says cd lab expects you to start in your home directory. cd ~/lab works from anywhere.
One .. too many.
learner@rootvolt:~/lab/Documents$ cat ../../todo.txtcat: ../../todo.txt: No such file or directorycat is speaking, and it names the path. Read the path from the left, starting at ~/lab/Documents: the first .. leads to ~/lab, the second to your home directory, and your home directory has no todo.txt. The file is one level up, so the path needs one ..: cat ../todo.txt.
Security Angle
Section titled “Security Angle”Programs that send files to other people, such as web servers, often build a path from a fixed directory and a name that the visitor sends. If the program does not check that name, a visitor can climb out of the directory with ...
Take a server that shares the files in /var/www/files. A visitor asks for ../../../etc/passwd, and the server builds the path /var/www/files/../../../etc/passwd. From the left, the three .. lead up to /var/www, /var, and /, so the path names /etc/passwd, the system’s list of user accounts. The server was never meant to share it. This weakness is called path traversal, and it has its own entry in MITRE’s list of software weaknesses: CWE-22. Reading a path one name at a time shows where it leads.
Try it yourself
Section titled “Try it yourself”Goal: Name a file from a directory this lesson did not use, and move between the lab’s two directories with one cd each.
- Open a new terminal. Move to
/homewith onecdand an absolute path, and check where you are. - Stay in
/home, and printreport.txtwith a relative path. - Move into
~/lab/projectswith onecdand a relative path: do not start it with/or~. - From
projects, move into~/lab/Documentswith onecdand a relative path, and check where you are.
Expected: Step 1 prints /home, and step 2 prints Quarterly lab report. At the end, pwd prints /home/learner/lab/Documents, and the prompt ends in ~/lab/Documents$.
Hint
In /home, a relative path starts with a directory inside /home; lesson B05.1 listed it with ls /home. projects and Documents have the same parent.
Solution
learner@rootvolt:~$ cd /homelearner@rootvolt:/home$ pwd/homelearner@rootvolt:/home$ cat learner/lab/Documents/report.txtQuarterly lab reportlearner@rootvolt:/home$ cd learner/lab/projectslearner@rootvolt:~/lab/projects$ pwd/home/learner/lab/projectslearner@rootvolt:~/lab/projects$ cd ../Documentslearner@rootvolt:~/lab/Documents$ pwd/home/learner/lab/DocumentsFrom /home, the relative path to the file is learner/lab/Documents/report.txt: down through learner, lab, and Documents. The same start gives cd learner/lab/projects. From projects, .. leads up to lab, and Documents down again.
- A path names a file or directory, with
/between the names. Read it from the left, one name at a time. - An absolute path starts with
/and names every directory from the root down. It means the same file from anywhere, andpwdprints one. - A relative path starts in the current directory, so it can name a different file, or nothing, from another directory. Inside it,
..leads one level up and.stays where you are. - Bash replaces
~at the start of a word with your home directory, so~/lab/todo.txtreaches the program as the absolute path/home/learner/lab/todo.txt. cdtakes any path, so onecdreaches any directory.
Next, lesson B05.3, Listing and moving around, practices moving between directories and the common views of ls, including hidden files.
Further reading
Section titled “Further reading”- POSIX Base Definitions: 3.2 Absolute Pathname and 3.311 Relative Pathname, and 4.16 Pathname Resolution, the rules for following a path one name at a time.
man 7 path_resolutionon your system, or the Ubuntu 24.04 path_resolution(7) page online: how Linux finds the file a path names, including.and...- GNU Bash Reference Manual: Tilde Expansion, how Bash replaces
~before a command runs. - GNU Bash Reference Manual: Bourne Shell Builtins, the entry for
cd. On your system,help cdprints a short version.