Absolute vs Relative Paths

There are two ways to specify where a file is:

  1. Absolute path - The full address from root
  2. 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.

Terminal
$# These work from any directory:
$cat /etc/hostname
debian
$ls /home/user
Documents Downloads projects

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

Terminal
$pwd
/home/user
$# From /home/user, this relative path works:
$ls projects
website app scripts
$
$# But from /tmp, it wouldn't:
$cd /tmp && ls projects
ls: cannot access 'projects': No such file or directory

Special Directory References

These shortcuts make relative paths powerful:

SymbolMeaning
.Current directory
..Parent directory (one level up)
~Home directory
-Previous directory
Terminal
$pwd
/home/user/projects
$ls .
website app scripts
$ls ..
Documents Downloads projects
$ls ~
Documents Downloads projects
$ls ../Documents
report.pdf notes.txt

The .. shortcut is incredibly useful for moving up the tree:

hljs bash
# 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
Terminal
$pwd
/home/user/projects/website/src
$cd ..
$pwd
/home/user/projects/website
$cd ../..
$pwd
/home/user/projects

The . Shortcut

. means "current directory." You'll see it most often when:

  1. Running executables in the current directory:
hljs bash
./script.sh     # Run script.sh in current directory
  1. Copying to current location:
hljs bash
cp /etc/config.conf .    # Copy to current directory

Which Should You Use?

Use CaseBest Choice
Shell scriptsAbsolute paths
Config filesAbsolute paths
Quick navigationRelative paths
Typing in terminalWhichever 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) vs cat /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 Documents and cd documents are different!
hljs bash
# 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.

Knowledge Check

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.