Filesystem Hierarchy
Linux has no drive letters. Everything, every disk, USB stick and even running processes, appears in one tree that starts at the root directory /. The layout of that tree follows the Filesystem Hierarchy Standard (FHS), so once you know it you can find your way around any distribution.
The top-level directories
| Directory | Contains |
|---|---|
/ |
The root of everything |
/bin, /usr/bin |
Programs for all users (ls, grep, python3) |
/sbin, /usr/sbin |
System administration programs |
/etc |
System-wide configuration (text files) |
/home |
Users' home directories (/home/ada) |
/root |
The root user's home |
/var |
Variable data: logs, caches, databases, mail, spool |
/tmp |
Temporary files, often cleared at reboot |
/usr |
Installed software: programs, libraries, docs (read-only in spirit) |
/usr/local |
Software you installed yourself, outside the package manager |
/opt |
Self-contained third-party applications |
/lib, /usr/lib |
Shared libraries and kernel modules |
/dev |
Device files: disks, terminals, /dev/null |
/proc, /sys |
Virtual files showing kernel and process information |
/mnt, /media |
Mount points for extra disks and removable media |
/boot |
The kernel and boot loader files |
/srv |
Data served by this machine (web sites, FTP) |
/run |
Runtime data since the last boot (PID files, sockets) |
On modern distributions /bin, /sbin and /lib are usually symbolic links into /usr (the "usr merge").
for d in /etc /home /tmp /usr/bin /var /dev /proc; do
if [ -d "$d" ]; then printf '%-9s exists\n' "$d"; fi
done
echo "ls is $(command -v ls), env is $(command -v env)"/etc exists
/home exists
/tmp exists
/usr/bin exists
/var exists
/dev exists
/proc exists
ls is /bin/ls, env is /usr/bin/env
/etc: configuration
Almost every setting on a Linux system is a text file under /etc, which is why the text tools of this course are so useful for administration:
| File | Purpose |
|---|---|
/etc/passwd, /etc/group |
Users and groups |
/etc/hostname, /etc/hosts |
Machine name, static name lookups |
/etc/resolv.conf |
DNS servers |
/etc/fstab |
Filesystems to mount at boot |
/etc/ssh/sshd_config |
SSH server settings |
/etc/os-release |
Distribution name and version |
/etc/systemd/, /etc/cron.d/ |
Services and scheduled jobs |
cat /etc/hostname
head -n 1 /etc/passwd
grep '^ID=' /etc/os-releasetad
root:x:0:0:root:/root:/bin/sh
ID=alpine
This sandbox runs Alpine Linux; on Ubuntu the last line would print ID=ubuntu, on Fedora ID=fedora.
/var/log: logs
Services write logs below /var/log (syslog or messages, auth.log, nginx/, …). On systemd machines many logs live in the journal instead, read with journalctl (cron & systemd lesson). Big, growing log files are one of the most common causes of a full disk; logrotate compresses and deletes old ones.
/dev: everything is a file
Devices appear as special files. Programs read and write them like normal files:
| Device | Behaviour |
|---|---|
/dev/null |
Discards writes, reads as empty |
/dev/zero |
Endless zero bytes |
/dev/urandom |
Endless pseudo-random bytes |
/dev/sda, /dev/nvme0n1 |
Whole disks; sda1, nvme0n1p1 are partitions |
/dev/tty |
The current terminal |
stat -c '%F: %n' /dev/null /dev/zero /etc/passwd /tmp
head -c 6 /dev/zero | od -An -tx1
echo "this vanishes" > /dev/null
wc -c < /dev/nullcharacter special file: /dev/null
character special file: /dev/zero
regular file: /etc/passwd
directory: /tmp
00 00 00 00 00 00
0
od -tx1 shows bytes in hexadecimal: six zero bytes.
/proc: the kernel's live view
Files in /proc don't exist on disk: the kernel generates their content when you read them. /proc/self always refers to the process reading it:
grep -E '^(Name|State|Umask)' /proc/self/status
cat /proc/1/comm
[ -r /proc/cpuinfo ] && echo "CPU details are in /proc/cpuinfo"
[ -r /proc/meminfo ] && echo "memory details are in /proc/meminfo (used by free)"Name: grep
Umask: 0022
State: R (running)
bash
CPU details are in /proc/cpuinfo
memory details are in /proc/meminfo (used by free)
Tools such as ps, top and free are largely friendly readers of /proc.
Mounting
Additional filesystems are attached ("mounted") onto directories. /etc/fstab lists the ones to mount at boot; mount and findmnt show what's mounted now (sample output, requires the real system):
$ findmnt -t ext4,vfat
TARGET SOURCE FSTYPE OPTIONS
/ /dev/nvme0n1p2 ext4 rw,relatime
/boot/efi /dev/nvme0n1p1 vfat rw,relatime
$ sudo mount /dev/sdb1 /mnt/usb
$ sudo umount /mnt/usbPaths you'll use daily
| Path | Why |
|---|---|
~/.bashrc, ~/.profile |
Your shell configuration |
~/.ssh/ |
SSH keys and config |
~/.config/ |
Settings of most desktop and CLI apps (XDG standard) |
~/.local/bin |
Your own programs (add it to PATH) |
/var/log/ |
Logs |
/etc/ |
System configuration |
Test yourself
Where are system-wide configuration files kept?
- /usr
- /etc
- /var
- /opt
Where would you look for a web server's log files?
- /etc/log
- /var/log
- /tmp/log
- /usr/log
What is special about the files in /proc?
- They are compressed
- They are generated by the kernel on the fly and don't exist on disk
- Only root can read them
- They are backups