Absolute vs Relative Paths

An absolute path starts from the root /, while a relative path starts from where you are now.

A path is the address of a file or directory. There are two ways to write one.

Absolute path

Starts at the root of the filesystem with a leading slash /. It is unambiguous — it means the same thing from anywhere.

/home/alice/documents/report.txt

Relative path

Starts from your current directory, with no leading slash. Two special names help:

  • . — the current directory.
  • .. — the parent directory (one level up).
  • ~ — your home directory.

If you are in /home/alice, then documents/report.txt points to the same file as the absolute path above.

Example

Example · bash
pwd
# /home/alice

# Absolute path — works from anywhere
cat /home/alice/documents/report.txt

# Relative path — same file, because we are in /home/alice
cat documents/report.txt

# '..' climbs up: read a file in the parent directory
cat ../shared.txt

When to use it

  • A shell script uses absolute paths like '/usr/bin/python3' to ensure it runs the correct interpreter regardless of the caller's current directory.
  • A developer navigates quickly with '../../config' relative to their current source file during a multi-level project structure refactor.
  • A sysadmin uses 'realpath symlink_name' to discover the actual target of a symbolic link before editing it.

More examples

Absolute vs relative path

Contrasts absolute paths starting from root '/' with relative paths that depend on the current directory.

Example · bash
# Absolute path (always works from anywhere)
cat /etc/nginx/nginx.conf

# Relative path (works only from /etc/nginx)
cd /etc/nginx
cat nginx.conf

Use dot-dot to navigate up

Uses '../..' to traverse up two directory levels and reach a sibling folder without an absolute path.

Example · bash
pwd
# /home/alice/projects/api/src

# Go up two levels then into a sibling directory
ls ../../tests/
# integration/  unit/

Resolve symlinks and tilde

Shows that tilde is a shell shortcut for home and 'realpath' dereferences symlinks to their target.

Example · bash
# ~ expands to the home directory
echo ~
# /home/alice

# Resolve the real path behind a symlink
realpath /usr/bin/python3
# /usr/bin/python3.12

Discussion

  • Be the first to comment on this lesson.