Skip to content

Absolute and relative paths

Module
Navigating the Filesystem
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21
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.

Screen
learner@rootvolt:~/lab/Documents$ cat notes.txt
Buy printer paper
Renew lab VM snapshot
learner@rootvolt:~/lab/Documents$ cd ..
learner@rootvolt:~/lab$ cat todo.txt
Install Ubuntu
Verify the ISO checksum
Take a snapshot
learner@rootvolt:~/lab$ cd Documents

It 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.

Open a terminal and run cd ~/lab, then cd Documents. Your prompt ends in ~/lab/Documents$.

You already know one name for this directory that starts at the top. Ask for it:

Terminal window
pwd
Output
/home/learner/lab/Documents

Read 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:

Terminal window
cat /home/learner/lab/todo.txt
Output
Install Ubuntu
Verify the ISO checksum
Take a snapshot

The prompt still ends in ~/lab/Documents$: you did not move. Now go to the top, and run the same command again:

Terminal window
cd /
cat /home/learner/lab/todo.txt
Output
Install Ubuntu
Verify the ISO checksum
Take a snapshot

The 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:

Terminal window
ls /home/learner/lab
Output
Documents projects todo.txt

Go back with cd ~/lab. In module B04, you ran commands like this one there:

Terminal window
cat Documents/notes.txt
Output
Buy printer paper
Renew lab VM snapshot

Documents/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?

Terminal window
cd projects
cat Documents/notes.txt
Run it, then compare with your guess
Error output
cat: Documents/notes.txt: No such file or directory

The 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.

You are in projects, and you still want notes.txt. .., the parent directory from lesson B05.1, also works inside a path:

Terminal window
cat ../Documents/notes.txt
Output
Buy printer paper
Renew lab VM snapshot

Read 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.

Two routes to ~/lab/Documents/notes.txt 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 the current directory ~/lab/projects, goes up with .. to lab, then down through Documents. Both end at the same file, notes.txt. / home learner lab Documents projects notes.txt Absolute path starts at / Absolute path, from anywhere /home/learner/lab/Documents/notes.txt .. Relative path starts at the current directory Relative path, from ~/lab/projects ../Documents/notes.txt
Two routes to ~/lab/Documents/notes.txt 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 the current directory ~/lab/projects, goes up with .. to lab, then down through Documents. Both end at the same file, notes.txt. / home learner lab Documents projects notes.txt Absolute path starts at / Absolute path, from anywhere /home/learner/lab/Documents/notes.txt .. Relative path starts at the current directory Relative path, from ~/lab/projects ../Documents/notes.txt
Two routes to the same file: the absolute path starts at /, and the relative path starts at the current directory, ~/lab/projects.

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:

Screen
learner@rootvolt:~/lab/Documents$ cat ../todo.txt
Install Ubuntu
Verify the ISO checksum
Take a snapshot

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:

Terminal window
cd ..
cat ./todo.txt
Output
Install Ubuntu
Verify the ISO checksum
Take a snapshot

./todo.txt means “todo.txt in this directory”, the same file as todo.txt. For cat, the ./ changes nothing.

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.

One file, two names, two working directories
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--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21. From ~/lab and from /, two different paths name the same file, ~/lab/todo.txt.
Steps (9)
  1. Type cat todo.txt — Relative: read from the current directory, ~/lab
  2. Press Enter
  3. Output: Install Ubuntu Verify the ISO checksum Take a snapshot
  4. Type cd / — Move to the root directory
  5. Press Enter
  6. Type cat /home/learner/lab/todo.txt — Absolute: the whole route from /
  7. Press Enter
  8. Output: Install Ubuntu Verify the ISO checksum Take a snapshot
  9. 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.

~/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:

Terminal window
echo ~/lab/todo.txt
Output
/home/learner/lab/todo.txt

The 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 /:

Screen
learner@rootvolt:/$ cat ~/lab/todo.txt
Install Ubuntu
Verify the ISO checksum
Take a snapshot

So ~/… is not a third kind of path. It is a short way to type an absolute path that starts in your home directory.

cd takes a path like any other command, absolute or relative. The four cd commands from the opening become one:

Terminal window
cd /
cd /home/learner/lab/projects
pwd
Output
/home/learner/lab/projects

From projects, a relative path with .. reaches Documents, which has the same parent:

Terminal window
cd ../Documents
pwd
Output
/home/learner/lab/Documents

And cd ~/lab brings you back to the lab from anywhere:

Terminal window
cd ~/lab
pwd
Output
/home/learner/lab

A 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.

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.

Screen
learner@rootvolt:~/lab$ cat /Documents/notes.txt
cat: /Documents/notes.txt: No such file or directory

cat 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.

Screen
learner@rootvolt:~/lab$ cd lab
bash: cd: lab: No such file or directory

Bash 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.

Screen
learner@rootvolt:~/lab/Documents$ cat ../../todo.txt
cat: ../../todo.txt: No such file or directory

cat 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.

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.

Goal: Name a file from a directory this lesson did not use, and move between the lab’s two directories with one cd each.

  1. Open a new terminal. Move to /home with one cd and an absolute path, and check where you are.
  2. Stay in /home, and print report.txt with a relative path.
  3. Move into ~/lab/projects with one cd and a relative path: do not start it with / or ~.
  4. From projects, move into ~/lab/Documents with one cd and 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
Screen
learner@rootvolt:~$ cd /home
learner@rootvolt:/home$ pwd
/home
learner@rootvolt:/home$ cat learner/lab/Documents/report.txt
Quarterly lab report
learner@rootvolt:/home$ cd learner/lab/projects
learner@rootvolt:~/lab/projects$ pwd
/home/learner/lab/projects
learner@rootvolt:~/lab/projects$ cd ../Documents
learner@rootvolt:~/lab/Documents$ pwd
/home/learner/lab/Documents

From /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, and pwd prints 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.txt reaches the program as the absolute path /home/learner/lab/todo.txt.
  • cd takes any path, so one cd reaches any directory.

Next, lesson B05.3, Listing and moving around, practices moving between directories and the common views of ls, including hidden files.