Table of Contents
Understanding Absolute vs Relative Paths
When working with files and directories, you constantly refer to paths. In Linux, these paths come in two main forms:
- Absolute paths — always start from the root
/ - Relative paths — start from your current directory
This chapter focuses on how these two types of paths work, how to recognize them, and how to use them correctly.
Absolute Paths
An absolute path always starts from the root directory / and describes the full route to a file or directory.
Key characteristics:
- Always starts with
/ - Does not depend on where you currently are
- Is the same from any directory
Examples:
/home/alex/Documents/report.txt/etc/ssh/sshd_config/var/log/syslog/usr/bin/python3
You can think of an absolute path as a full street address including country, city, and house number: it uniquely identifies a location on the system.
When to use absolute paths
Absolute paths are useful when:
- Writing scripts that should work no matter where they are run from
- Referring to system directories like
/etc,/var,/usr,/bin - Avoiding confusion about which file or directory you’re talking about
Relative Paths
A relative path is interpreted starting from your current directory (often called the current working directory).
Key characteristics:
- Does not start with
/(that’s the simplest way to recognize it) - Depends on where you are (
pwd) - Shorter and often more convenient when working in one part of the filesystem
Examples (assume pwd shows /home/alex):
Documents/report.txt
Refers to/home/alex/Documents/report.txtDownloads
Refers to/home/alex/Downloadsproject/src/main.c
Refers to/home/alex/project/src/main.c
If you move to a different directory, the same relative path may point somewhere else or not exist at all.
Special entries: `.` and `..`
Two special directory names are essential for relative paths:
.— the current directory..— the parent directory (one level up)
They are most useful inside relative paths:
./script.sh
Runscript.shin the current directory../notes.txt
Go up one directory, then accessnotes.txt../../Photos
Go up two directories, then intoPhotos
These are always relative; you will not see valid absolute paths like /../home.
Recognizing Absolute vs Relative Quickly
You can identify the type of path just by looking at the first character:
- If it starts with
/, it’s an absolute path - Otherwise, it’s a relative path
Examples — classify each:
/home/user/file.txt→ absolutehome/user/file.txt→ relative./file.txt→ relative../file.txt→ relative/etc/../var/log→ absolute (still starts with/, even though it contains..)
Note: tools will internally simplify paths like /etc/../var/log to /var/log.
How the Shell Interprets Paths
The shell (like bash) interprets paths roughly like this:
- For an absolute path:
- Start at
/ - Follow each directory component
- For a relative path:
- Start at the current working directory (shown by
pwd) - Then follow each directory component, including any
.and..
You can always see the full absolute path of your current directory using:
pwd
From that, you can mentally expand a relative path into its absolute form by “appending” it to the output of pwd and simplifying any . or ...
Example:
pwd→/home/alex/projects/webapp- Relative path:
../docs/readme.md - Combine:
/home/alex/projects/webapp/../docs/readme.md - Simplify
..:/home/alex/projects/docs/readme.md(this is the absolute path)
Common Uses and Differences in Practice
Using `cd` with absolute vs relative paths
cd /var/log
Go to/var/logfrom anywhere (absolute)cd log
Go to a subdirectory namedloginside your current directory (relative)cd ..
Go one level up (relative)cd ../..
Go up two levels (relative)
Using commands that accept paths
Commands like ls, cp, mv, rm, cat, etc., all accept both absolute and relative paths:
ls /etc— list/etc(absolute)ls ..— list parent of current directory (relative)cp notes.txt ../backup/— copy from current dir to parent’sbackup/(relative)cp /home/alex/notes.txt /tmp/— copy using only absolute paths
The command doesn’t care which form you use; you must understand from where a relative path is evaluated.
Converting Between Absolute and Relative Paths (Conceptually)
You often need to “convert” between path types in your head:
From absolute to relative
Given:
- Current directory:
/home/alex/projects/webapp - Target file:
/home/alex/projects/docs/readme.md
You can move up to the common ancestor (/home/alex/projects) and then down:
- From
/home/alex/projects/webappup one →..=/home/alex/projects - Then into
docs/readme.md
So a relative path from webapp to that file is:
../docs/readme.md
From relative to absolute
Given:
- Current directory:
/home/alex/projects/webapp - Relative path:
../docs/readme.md
Combine and simplify:
/home/alex/projects/webapp/../docs/readme.md
→/home/alex/projects/docs/readme.md
Choosing Which Type to Use
Guidelines:
- Use absolute paths when:
- Writing scripts or system configuration
- Referring to system-wide files and directories
- You need clarity and reliability regardless of current directory
- Use relative paths when:
- Working interactively in one area of the filesystem
- You want shorter commands
- Navigating around a project or your home directory
In many real-world situations, you mix both: an absolute path for a destination and a relative path for a source, or vice versa.
Practice Ideas
To become comfortable with absolute and relative paths, try:
- Use
pwdto see where you are. - From your home directory:
- Use
cdwith an absolute path to go to/var/log. - Go back using
cdand a relative path only (..etc.). - Inside a nested project directory (e.g.
~/project/src/module), figure out: - The absolute path to a file in
../docs - A relative path from
docsback tosrc/module
Regular practice like this will make reading and writing both types of paths feel natural.