TExcelTAD
Home › Operating Systems: Linux & Windows › The Linux Directory Tree

The Linux Directory Tree

Windows gives every disk its own letter: C:\, D:\, E:\. Linux has one tree that starts at / (the root), and every disk, partition, USB stick, network share and virtual file system is attached somewhere inside it by mounting. Where things go in that tree is not random: it follows the Filesystem Hierarchy Standard (FHS), so a path like /etc/ssh/sshd_config means the same thing on Ubuntu, Fedora, Arch or Alpine.

This lesson is about why the tree looks the way it does. For a hands-on tour with more commands, see Bash: Filesystem Hierarchy.

The top level

text
/
├── bin  -> usr/bin     user commands (merged into /usr on most distros)
├── boot                kernel, initramfs, GRUB files
├── dev                 device files (devtmpfs, created by the kernel + udev)
├── etc                 system-wide configuration, text files
├── home                users' home directories
├── lib  -> usr/lib     shared libraries, kernel modules
├── media, mnt          mount points for removable and temporary disks
├── opt                 self-contained third-party software
├── proc                processes and kernel state (virtual, procfs)
├── root                home of the root user
├── run                 runtime data since boot (tmpfs)
├── sbin -> usr/sbin    administration commands
├── srv                 data this machine serves (web, FTP)
├── sys                 devices and drivers (virtual, sysfs)
├── tmp                 temporary files
├── usr                 installed software: bin, lib, share, include, local
└── var                 variable data: logs, caches, spools, databases

The course sandbox is a small Alpine Linux container. Let's check which of the standard directories it has, with their permissions and owners:

bash · try it yourself
for d in /bin /boot /dev /etc /home /lib /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var; do
  if [ -e "$d" ]; then
    stat -c '%A %-5U %n' "$d"
  else
    echo "(missing)       $d"
  fi
done
drwxr-xr-x root  /bin
(missing)       /boot
drwxr-xr-x root  /dev
drwxr-xr-x root  /etc
drwxr-xr-x root  /home
drwxr-xr-x root  /lib
drwxr-xr-x root  /media
drwxr-xr-x root  /mnt
drwxr-xr-x root  /opt
dr-xr-xr-x root  /proc
drwx------ root  /root
drwxr-xr-x root  /run
drwxr-xr-x root  /sbin
drwxr-xr-x root  /srv
dr-xr-xr-x root  /sys
drwxrwxrwt root  /tmp
drwxr-xr-x root  /usr
drwxr-xr-x root  /var

Three things to notice:

  • /boot is missing: a container shares the host's kernel and never boots, so it needs no kernel files.
  • /root is drwx------: only root may even look inside the administrator's home.
  • /tmp is drwxrwxrwt: everyone may create files there, and the t (sticky bit) stops users from deleting each other's files.

Sorting files by four questions

The FHS is easier to remember once you see the two questions behind most of it: is the data shareable between machines or local to this host, and is it static (changes only when an admin installs something) or variable (changes while the system runs)?

Static Variable
Shareable /usr, /opt /var/mail, /var/spool/news
Local to this host /etc, /boot /var/run (now /run), /var/lock, /var/log

That is why /usr can in principle be mounted read-only or shared over the network, and why everything that grows (logs, caches, package databases, mail queues) lives under /var, often on its own partition so a runaway log cannot fill the root file system.

Two more questions settle the rest: who manages it and does it survive a reboot?

Directory Managed by Survives reboot
/usr/bin, /usr/lib the package manager (apt, dnf, pacman) yes
/usr/local, /opt you, by hand yes
/etc the admin (packages ship defaults) yes
/home the users yes
/run, /dev/shm programs at runtime (tmpfs) no
/tmp anyone often no (tmpfs or cleaned at boot)
/proc, /sys the kernel, generated on the fly not real files at all

The /usr merge

Historically /bin held the commands needed to boot and repair a system, while /usr/bin (on a second disk that was mounted later) held everything else. Today the initramfs handles early boot, so that split no longer makes sense. Fedora, Debian 12+, Ubuntu, Arch and openSUSE have performed the /usr merge: /bin, /sbin and /lib are just symlinks into /usr (sample output from Ubuntu 24.04):

bash
ada@ubuntu:~$ ls -ld /bin /sbin /lib
lrwxrwxrwx 1 root root 7 Apr 22  2024 /bin -> usr/bin
lrwxrwxrwx 1 root root 7 Apr 22  2024 /lib -> usr/lib
lrwxrwxrwx 1 root root 8 Apr 22  2024 /sbin -> usr/sbin

Alpine, used by the sandbox, still keeps them as real directories.

/etc: the text-file control panel

Almost every setting lives in a plain text file under /etc: users in /etc/passwd, hostname in /etc/hostname, mounts in /etc/fstab, the distribution's identity in /etc/os-release. Readable by everyone, writable only by root (except secrets such as /etc/shadow, which is not even readable):

bash · try it yourself
cat /etc/hostname
grep -E '^(NAME|ID)=' /etc/os-release
grep "^$(id -un):" /etc/passwd
stat -c '%A %U:%G %n' /etc/passwd /etc/shadow
touch /etc/test 2>&1 || true
tad
NAME="Alpine Linux"
ID=alpine
learner:x:1000:1000:learner:/home/learner:/bin/bash
-rw-r--r-- root:root /etc/passwd
-rw-r----- root:shadow /etc/shadow
touch: cannot touch '/etc/test': Permission denied

Compare that with Windows, where most of the same settings live in a binary database, the registry.

/dev: devices as files

"Everything is a file" is the Unix idea that devices, pipes and kernel information appear as names in the tree, so the same open/read/write calls work for all of them. Device files in /dev have no content of their own; their major and minor numbers tell the kernel which driver handles them:

bash · try it yourself
stat -c '%-10n %F, major %t, minor %T' /dev/null /dev/zero /dev/urandom /dev/tty
echo "this disappears" > /dev/null
head -c 4 /dev/zero | od -An -tx1
/dev/null  character special file, major 1, minor 3
/dev/zero  character special file, major 1, minor 5
/dev/urandom character special file, major 1, minor 9
/dev/tty   character special file, major 5, minor 0
 00 00 00 00

On a real machine /dev also contains your disks (/dev/nvme0n1, /dev/sda), partitions (/dev/nvme0n1p2) and stable aliases such as /dev/disk/by-uuid/… created by udev (see Devices & I/O).

Where do I put my stuff?

You want to… Put it in
Install a program by hand (one binary) /usr/local/bin (system-wide) or ~/.local/bin (just you)
Install a big vendor bundle /opt/<vendor>
Change a service's settings /etc/<service>/ (or a drop-in in /etc/<service>.d/)
Store a web site /srv/www or /var/www
Keep personal config ~/.config/<app> (the XDG convention)
Leave a temporary file /tmp or $XDG_RUNTIME_DIR (/run/user/1000)

Never put your own files in /usr/bin or /usr/lib: the package manager owns them and may overwrite them during the next update.

Learn more: Bash: Filesystem Hierarchy · The Windows Folder Layout · /proc & /sys

Test yourself

Which directory holds data that grows while the system runs, such as logs and caches?

  1. /usr
  2. /etc
  3. /var
  4. /opt

On Ubuntu 24.04, what is /bin?

  1. A separate partition
  2. A symlink to usr/bin (the /usr merge)
  3. An empty directory
  4. A tmpfs mount

Where should you install a single program you compiled yourself, for all users?

  1. /usr/bin
  2. /usr/local/bin
  3. /etc
  4. /var/lib