The Linux Filesystem Hierarchy

Beginner
10 min

The Linux Filesystem Hierarchy

Linux has no drive letters. Everything — disks, USB sticks, network shares, even running processes — appears somewhere under a single root directory called /. The layout is standardised by the Filesystem Hierarchy Standard (FHS), which is why a configuration file on Ubuntu is in the same place as on Fedora. This lesson maps out the important directories so you always know where to look, and explains absolute and relative paths.

One Tree, One Root

Windows has C:\ and D:\; Linux has only /. Additional disks are mounted onto directories inside the tree, so a second drive might appear as /data or /mnt/backup. Paths use forward slashes, are case-sensitive (Readme.md and readme.md are different files), and there is no file extension requirement — the system determines file types by content, not by name.

bash
ls /
bash
bin dev home lib64 mnt proc run snap sys usr boot etc lib media opt root sbin srv tmp var

The Directories You Will Use Daily

| Directory | Contents | Example | |---|---|---| | /home | One subdirectory per normal user | /home/alice/projects | | /root | Home directory of the root user | /root/.bashrc | | /etc | System-wide configuration, plain text | /etc/ssh/sshd_config, /etc/hosts | | /var | Variable data: logs, caches, databases, mail | /var/log/syslog, /var/lib/mysql | | /tmp | Temporary files, usually cleared on reboot | build artefacts, sockets | | /usr | Installed programs and their libraries | /usr/bin/python3, /usr/lib | | /opt | Optional third-party software in its own folder | /opt/google/chrome | | /srv | Data served by this machine | /srv/www |

/etc was historically "et cetera" but is best remembered as "editable text configuration". Almost every service you install drops a file or directory there, and most administrative work is editing those files.

System and Boot Directories

  • /bin, /sbin, /lib: essential binaries and libraries. On modern distributions these are symbolic links to /usr/bin, /usr/sbin and /usr/lib (the "usr merge"), so ls -l /bin shows bin -> usr/bin.
  • /boot: the kernel (vmlinuz-*) and bootloader files. Do not delete anything here.
  • /dev: device files. /dev/sda is the first disk, /dev/null discards anything written to it, /dev/urandom produces random bytes.
  • /proc and /sys: virtual filesystems generated by the kernel. cat /proc/cpuinfo shows CPU details; /proc/<pid>/ describes a running process. Nothing here is on disk.
  • /run: runtime state since boot, such as PID files and sockets.
  • /mnt and /media: conventional mount points for manual and removable media.
bash
cat /proc/meminfo | head -3 # memory stats straight from the kernel ls -l /dev/null # crw-rw-rw- ... /dev/null (c = character device) echo "gone" > /dev/null # output vanishes

Absolute and Relative Paths

An absolute path starts with / and means the same thing regardless of where you are. A relative path is interpreted from your current working directory. Two special names exist in every directory: . is the directory itself and .. is its parent.

bash
pwd # /home/alice cd projects/api # relative: /home/alice/projects/api cd ../../ # up two levels: /home/alice cd /etc/nginx # absolute cd ~/projects # ~ expands to /home/alice cd - # jump back to /etc/nginx

Use absolute paths in scripts, cron jobs and configuration files, because those run from directories you do not control. Use relative paths interactively to save typing.

Hidden Files and Dotfiles

Any file or directory whose name starts with . is hidden from a plain ls. Your home directory is full of them: .bashrc, .ssh/, .gitconfig, .config/. Show them with ls -a. They are ordinary files; the dot is just a naming convention that tools respect.

bash
ls -a ~ ls -ld ~/.ssh # -d lists the directory itself instead of its contents

Tips

  • When a service misbehaves, the pattern is: config in /etc/<service>, logs in /var/log/<service>, data in /var/lib/<service>, binary in /usr/bin or /usr/sbin.
  • Never store project files in /tmp; many systems wipe it on reboot or after a few days.
  • df -h / shows how full the root filesystem is; /var/log and /var/cache/apt are the usual culprits when it fills up.
  • Tab completion works on paths, so type cd /e, press Tab, and let the shell finish /etc/.
Quick Quiz
Question 1 of 3

Where would you expect to find the configuration file for the SSH server?

Key Takeaways

  • Linux has a single tree rooted at /; other disks are mounted onto directories inside it.
  • Configuration lives in /etc, logs and data in /var, programs in /usr, user files in /home.
  • /proc, /sys and /dev are interfaces to the kernel and hardware, not ordinary storage.
  • Absolute paths start with /; relative paths use . and .. from the current directory.
  • Names starting with . are hidden; ls -a reveals them.

Next lesson: Navigating the Filesystem: pwd, ls, cd — put the map to use by moving around the tree and listing what is in each directory.

The Linux Filesystem Hierarchy - Linux & Command Line | CodeYourCraft | CodeYourCraft