Absolute vs Relative Paths
There are two ways to specify where a file is:
- Absolute path - The full address from root
- Relative path - The address from where you currently are
Understanding the difference will save you from countless "file not found" errors.
Absolute Paths
An absolute path starts with / and describes the complete location from the root:
/home/user/projects/website/index.html
Absolute paths work from anywhere. It doesn't matter where you are - this path always points to the same file.
When to Use Absolute Paths
Use absolute paths in scripts and configs. They're unambiguous and won't break if someone runs the script from a different directory.
Relative Paths
A relative path describes location from your current directory. It does NOT start with /:
projects/website/index.html
This only works if you're in a directory that contains projects/.
Special Directory References
These shortcuts make relative paths powerful:
| Symbol | Meaning |
|---|---|
. | Current directory |
.. | Parent directory (one level up) |
~ | Home directory |
- | Previous directory |
Navigating with ..
The .. shortcut is incredibly useful for moving up the tree:
# You're in /home/user/projects/website/src/components
cd .. # Now in /home/user/projects/website/src
cd ../.. # Now in /home/user/projects/website
cd ../../.. # Now in /home/user/projects
The . Shortcut
. means "current directory." You'll see it most often when:
- Running executables in the current directory:
./script.sh # Run script.sh in current directory
- Copying to current location:
cp /etc/config.conf . # Copy to current directory
Which Should You Use?
| Use Case | Best Choice |
|---|---|
| Shell scripts | Absolute paths |
| Config files | Absolute paths |
| Quick navigation | Relative paths |
| Typing in terminal | Whichever is shorter |
The Rule
If it needs to work reliably regardless of where it's run from, use absolute paths. If you're just navigating interactively, use whatever's convenient.
Common Mistakes
Classic Errors to Avoid
- Missing
/at start:cat etc/passwd(relative) vscat /etc/passwd(absolute) - Confusing
.and..:.is current,..is parent.cd .goes nowhere,cd ..goes up - Too many
..: Can't go above root.cd /../../../..just stays at/ - Forgetting case sensitivity:
cd Documentsandcd documentsare different!
# You're in /home/user
cat /etc/passwd # Works - absolute path
cat etc/passwd # FAILS - relative path, no "etc" here
Missing that leading / is a classic beginner error. If a path doesn't start with /, ~, or ., Linux looks in your current directory.
If you're in /home/user/projects, what does `ls ../Documents` show?
Key Takeaways
- Absolute paths start with
/and work from anywhere - Relative paths are relative to your current directory
.means current directory,..means parent directory~means home directory,-means previous directory- Use absolute paths in scripts, relative paths for quick navigation
Next: let's practice navigation with pwd, ls, and cd.