Linux Basic — Absolute vs Relative Paths
Absolute vs Relative Paths
Every file on Linux has a unique address called its path. Understanding the difference between absolute and relative paths prevents confusion and is especially important when writing scripts.
Absolute Paths
An absolute path starts from the root directory / and gives the complete address of a file, regardless of where you currently are:
# These are absolute paths — they always mean the same thing:
/home/ubuntu/.bashrc
/etc/nginx/nginx.conf
/var/log/syslog
/usr/bin/python3
/tmp/myfile.txt
# Using an absolute path in commands
cat /etc/hostname # Works from ANY directory
ls /var/log # Works from ANY directory
# Verify:
cd /tmp
cat /etc/hostname # Still works even though we're in /tmp
Absolute paths are unambiguous — they point to exactly one file. This is why shell scripts should almost always use absolute paths.
Relative Paths
A relative path is interpreted from your current working directory (cwd):
# You are in: /home/ubuntu
pwd
# /home/ubuntu
ls projects # Same as ls /home/ubuntu/projects
cat .bashrc # Same as cat /home/ubuntu/.bashrc
cd Documents # Same as cd /home/ubuntu/Documents
# Now in /home/ubuntu/Documents
pwd
# /home/ubuntu/Documents
cat .bashrc # ERROR — .bashrc doesn't exist in /home/ubuntu/Documents
cat ../.bashrc # Works — goes up one level first
Special Path Symbols
| Symbol | Meaning |
|---|---|
. |
Current directory |
.. |
Parent directory |
~ |
Your home directory ($HOME) |
- |
Previous directory (used with cd) |
# . — current directory
ls . # List current directory (same as ls)
./myscript.sh # Run a script in the current directory
# .. — parent directory
cd .. # Go up one level
ls .. # List the parent directory
cat ../config # Read a file in the parent directory
cd ../.. # Go up two levels
# ~ — home directory (expands to /home/ubuntu for user ubuntu)
cd ~ # Go home
cat ~/.bashrc # Read your .bashrc (no matter where you are)
ls ~/projects/ # List your projects directory
# - — previous directory
cd /var/log
cd /etc/nginx
cd - # Go back to /var/log
Path Navigation Examples
# Given this directory structure:
# /home/ubuntu/
# ├── projects/
# │ ├── api/
# │ │ └── main.go
# │ └── frontend/
# │ └── index.html
# └── scripts/
# └── deploy.sh
# You are in: /home/ubuntu/projects/api
# Read main.go (relative)
cat main.go # Relative to current dir
# Read index.html (relative — go up one, then into frontend)
cat ../frontend/index.html
# Read deploy.sh (relative)
cat ../../scripts/deploy.sh
# Or absolute (clearer, unambiguous)
cat /home/ubuntu/scripts/deploy.sh
# What does this path resolve to?
# Current dir: /home/ubuntu/projects/api
# Path: ../../scripts/deploy.sh
# Step 1: ../ → /home/ubuntu/projects
# Step 2: ../../ → /home/ubuntu
# Step 3: ../../scripts → /home/ubuntu/scripts
# Step 4: ../../scripts/deploy.sh → /home/ubuntu/scripts/deploy.sh
Paths in Shell Scripts
In scripts, always use absolute paths or calculate paths relative to the script’s location:
#!/usr/bin/env bash
# BAD: relative paths in scripts are fragile
# This only works if the script is run from /home/ubuntu/
cat config/app.conf
# If run from /tmp, it looks for /tmp/config/app.conf — FAILS
# GOOD: absolute paths
cat /home/ubuntu/myapp/config/app.conf
# GOOD: paths relative to the script's own location
# $( ) and dirname give the directory where the script lives
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cat "${SCRIPT_DIR}/config/app.conf"
# This works no matter WHERE the script is called from
# GOOD: use environment variables for config paths
APP_HOME="/opt/myapp"
cat "${APP_HOME}/config/app.conf"
Resolving Paths
# realpath — resolve a path to its canonical absolute form
realpath ../frontend/index.html
# /home/ubuntu/projects/frontend/index.html
# readlink -f — same, and also resolves symlinks
readlink -f /usr/bin/python3
# /usr/bin/python3.10 (resolves the symlink chain)
# pwd -P — print the physical path (resolves symlinks in cwd)
cd /var/run
pwd
# /var/run
pwd -P
# /run (because /var/run → /run on modern Linux)
Practical Examples
# Copy a file from wherever you are to an absolute destination
cp report.pdf /home/ubuntu/Documents/
# Reference a config file using ~ (safe in interactive shell, avoid in scripts)
vim ~/.ssh/config
# Script using SCRIPT_DIR pattern
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG="${SCRIPT_DIR}/config/settings.env"
LOG="${SCRIPT_DIR}/logs/app.log"
source "$CONFIG"
echo "$(date): script started" >> "$LOG"
Key Takeaways
- Absolute paths start with
/— they always mean the same thing regardless of cwd - Relative paths are interpreted from the current directory (
pwd) .= current directory;..= parent;~= home;-= previous (forcd)- In shell scripts, always use absolute paths or the
$SCRIPT_DIRpattern realpathandreadlink -fconvert relative or symlink paths to canonical absolutes
| ← Back to FHS Directory Hierarchy | Next → File Types and Inodes |