LearnWithShobhan

Absolute vs relative path in Linux: the Unix filesystem explained

Shobhan Biswas··4 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

Every command you type in a terminal works on a path: cd into one, ls one, copy a file from one to another. If paths feel confusing, every other command feels confusing too. The good news is that the whole idea fits in one diagram.

Everything hangs off one root

Early computers used a flat file system: every file sat in one single place. You can imagine how fast that became a mess. Around 1965 the Multics operating system introduced a hierarchical file system, directories inside directories, and Unix adopted it in 1971. Linux and macOS inherited it from Unix.

There is one important difference from Windows. Windows gives each drive or partition its own letter (C:, D:), each with its own tree. Unix has a single tree with a single root, written as /. Plug in a USB drive and it shows up as a directory somewhere under that same root.

Unix: one tree under a single root. Windows: separate trees per drive letter
Unix has one tree. Windows has one tree per drive letter.

A simplified view of the directories you will see on most systems:

Unix filesystem tree: root with bin, boot, dev, etc, home, lib, tmp, usr, var; home contains john and bob
The Unix filesystem hierarchy (simplified). Names can differ a little between Ubuntu, CentOS and macOS.
DirectoryWhat lives there
/binEssential programs (binaries) like ls and cp
/bootFiles needed to boot the system
/devDevices, such as drives
/etcSystem-wide configuration
/homeOne directory per user (/Users on macOS)
/libShared libraries
/tmpTemporary files
/usrSoftware installed for users, with its own bin and lib
/varData that changes, like logs

Absolute path vs relative path

Take the videos directory inside John's home folder.

An absolute path describes the full route from the root. It always starts with /:

/home/john/videos

Every file and directory has exactly one absolute path. It works no matter where you currently are.

A relative path describes the route from where you are right now (your present working directory). If you are already inside /home/john, the file photo-3 is simply:

photos/photo-3
Tree diagram marking /home/john/videos as an absolute path and photos/photo-3 as a relative path from john
Absolute paths start at the root. Relative paths start where you are.

Watch it explained with this exact diagram in the free lesson:

Free to watch. Get all 57 lessons →

Move around: pwd, cd and ls

Three commands do all the navigation:

  • pwd (present working directory) prints where you are, as an absolute path.
  • cd (change directory) moves you, using an absolute or relative path.
  • ls (list) shows what is inside a directory.
Desktop — zsh
$ pwd
/Users/shobhan/Desktop
$ ls
animal dir-1 dir-2 file-1.txt
$ cd dir-1
$ pwd
/Users/shobhan/Desktop/dir-1

Both of these land in the same place. The first works from anywhere, the second only from inside Desktop:

cd /Users/shobhan/Desktop/dir-1   # absolute
cd dir-1                          # relative

Press Tab while typing a path and the shell autocompletes it. It saves typing and catches typos.

The five shortcuts worth memorising

Table: root is /, your home is ~, current directory is ., parent is .., previous is -
Five symbols you will use every day.
SymbolMeansExample
/The root directorycd /
~Your home directorycd ~ or cd ~/Desktop
.The current directory./deploy.sh, cd ./dir-2
..The parent directorycd ..
-The previous directory you were incd -

Run ls -la in any directory, even an empty one, and you will see . and .. listed. They are real entries that point to the directory itself and its parent.

Key takeaways

  • ✓Unix has one filesystem tree with a single root, /.
  • ✓Absolute paths start with / and work from anywhere.
  • ✓Relative paths start from your present working directory (pwd).
  • ✓~ is home, . is here, .. is the parent, - is where you just were.

Keep going

If this clicked for you, the rest of CLI Mastery is taught the same way: one concept drawn on a diagram, then done live in the terminal. 57 lessons, lifetime access, and you can watch three of them free before you decide.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading