Absolute and relative paths
Every file on Linux has an address in the filesystem tree. Absolute paths start at / and are unambiguous from anywhere; relative paths start from the current working directory and are shorter but context-dependent. . is here, .. is parent, ~ is home.
You already know that pwd tells you where you are and cd moves you. But every command that touches a file — cp, mv, cat, rm — needs you to name that file. There are two fundamentally different ways to name a path, and mixing them up causes “No such file or directory” errors that feel mysterious until the distinction clicks.
By the end of this lesson you will be able to name any file on the system two ways: with an absolute path that works from anywhere, and with a relative path that is shorter but depends on your current position.
After this lesson you can construct an absolute path (starting with /) to reach any file from any working directory, construct a relative path using ., .., and directory names, and explain why the same relative path can point to different files depending on where you run it.
The filesystem tree has one root: /. Every path on Linux ultimately traces back to the single / directory. Under it sit /home, /etc, /var, /usr, and others. A file at /home/alice/notes.txt lives at that exact location in the tree — no ambiguity possible, no matter where you are standing.
/
├── home/
│ └── alice/
│ └── notes.txt
├── etc/
│ └── hosts
└── var/
└── log/An absolute path starts with / and spells out the full route from the root. Because it begins at the root, it means exactly the same thing regardless of your current directory:
cat /home/alice/notes.txt
cat /etc/hostsBoth work whether your working directory is /, /tmp, or /home/alice/projects. The path is self-contained.
A relative path starts from your working directory — it has no leading /. The shell prepends your current directory to resolve the path. If you are in /home/alice:
cat notes.txtThe shell resolves this as /home/alice/notes.txt. If you were in /tmp, the same command would look for /tmp/notes.txt — a completely different file, or a “No such file” error.
This is the footgun: relative paths change meaning when your working directory changes.
Three special names help you navigate without spelling out full paths.
.— the current directory itself.cat ./notes.txtis identical tocat notes.txt; the leading./just makes the “relative” intent explicit...— the parent directory. From/home/alice/projects,../notes.txtrefers to/home/alice/notes.txt.~— expands to your home directory (e.g./home/alice).~/notes.txtis always/home/alice/notes.txtregardless of where you are.
These can be chained: ../../etc/hosts means “go up two levels then down into etc/hosts.”
When to use absolute vs relative. Use an absolute path when you need the command to work from any directory — scripts, cron jobs, and commands run from unknown locations should almost always use absolute paths. Use a relative path for interactive work when you are already near the target and typing the full path would be tedious. ~ is a sweet spot: it looks relative but always resolves to the same place.
Naming the same file two ways.
Suppose your home directory is /home/alice and you have a file at /home/alice/projects/app/main.py.
From /home/alice/projects/app (you are right next to the file):
# relative — short
cat main.py
# absolute — verbose but bulletproof
cat /home/alice/projects/app/main.py
# tilde shorthand — always resolves to home
cat ~/projects/app/main.pyNow move to /tmp and try the relative path:
cd /tmp
cat main.pycat: main.py: No such file or directoryThe absolute and ~ variants still work perfectly from /tmp. The relative one fails because /tmp/main.py does not exist.
▸Why this works
On macOS the path rules are identical: / is the root, absolute paths start with /, and ./../~ work the same way. The home directory is /Users/alice instead of /home/alice, but ~ always expands correctly so code using ~ is portable between Linux and macOS.
▸Common mistake
A common mistake in shell scripts is using a relative path for a file that lives next to the script. If the script is called from a different working directory, the relative path breaks. Always use $(dirname "$0") or an absolute path constructed from it when the file must be next to the script:
# fragile — breaks if you call the script from /tmp
cat config.txt
# robust — works from anywhere
cat "$(dirname "$0")/config.txt"Your working directory is /var/log. You run: cat ../etc/hosts. Which file does the shell actually open?
Every file on Linux lives at a unique location in the single-rooted / tree. An absolute path starts with / and identifies the file unambiguously from any working directory. A relative path has no leading / and is resolved by prepending the current working directory — it is shorter but context-dependent. The special names . (current directory), .. (parent directory), and ~ (home directory) can appear anywhere in a relative path. For scripts and automated tasks, prefer absolute paths or ~-anchored paths to avoid “file not found” surprises when the working directory changes.
Practice
Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.
Something unclear?
Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.