dotlinux guide

Comprehensive Exploration of the Linux Filesystem Hierarchy

The Linux filesystem hierarchy is a fundamental pillar of the operating system, organizing files and directories in a standardized, hierarchical structure. Unlike Windows, which uses drive letters (e.g., C:, D:), Linux abstracts storage devices into a single tree rooted at / (the root directory). This design ensures consistency across distributions, simplifies system administration, and enables interoperability between applications and users. Whether you’re a developer, system administrator, or casual Linux user, understanding the filesystem hierarchy is critical for tasks like configuring software, troubleshooting issues, managing storage, and securing the system. This blog explores the hierarchy in depth, from core directories to best practices, with practical examples to reinforce key concepts.

Table of Contents

  1. Fundamental Concepts
    • The Filesystem Hierarchy Standard (FHS)
    • Hierarchical Structure & Key Principles
  2. Core Directories Explained
    • Root Directory (/) and Essential Subdirectories
  3. Practical Usage Methods
    • Navigating the Filesystem
    • Monitoring Disk Usage
    • Mounting Filesystems
  4. Common Practices
    • File Organization
    • Symlinks and Hard Links
    • Mounting External Devices
  5. Best Practices for System Management
    • Permissions and Security
    • Backup Strategies
    • Avoiding Common Pitfalls
  6. Conclusion
  7. References

Fundamental Concepts

The Filesystem Hierarchy Standard (FHS)

The Linux filesystem follows the Filesystem Hierarchy Standard (FHS), a set of guidelines maintained by the Linux Foundation. The FHS defines the structure and purpose of directories, ensuring consistency across distributions (e.g., Ubuntu, Fedora, Debian). This standardization means a configuration file like /etc/passwd (user account data) exists in the same location regardless of the Linux flavor, simplifying cross-distribution documentation and tooling.

Hierarchical Structure & Key Principles

  • Single Root: All directories branch from the root directory (/). There are no “drives” (e.g., C:\); storage devices are mounted as subdirectories of /.
  • Everything is a File: Linux abstracts hardware, processes, and data as files. For example, /dev/sda represents a physical disk, and /proc/1 provides data about the first process (PID 1).
  • Static vs. Dynamic Data: Directories are divided into those containing static, read-only data (e.g., /usr, /boot) and dynamic, writable data (e.g., /var, /tmp).
  • Read-Only vs. Read-Write: Critical system directories (e.g., /boot) may be mounted as read-only (ro) to prevent accidental modification, while others (e.g., /home) are read-write (rw).

Core Directories Explained

Below is a detailed breakdown of the most important directories in the Linux filesystem, organized by their role.

/ (Root Directory)

The top-level directory from which all other directories branch. It contains only subdirectories (no files) and is typically small, as it only holds critical system directories.

# List contents of the root directory
ls -l /
# Output (varies by distro but includes key subdirs):
# bin   dev  home  lib32  libx32      mnt  proc  run   srv  tmp  var
# boot  etc  lib   lib64  lost+found  opt  root  sbin  sys  usr

/bin (Essential User Binaries)

Contains essential command-line binaries required for system repair and basic operation, even if no other filesystems are mounted. These binaries are available to all users.

Examples: ls, cp, mv, rm, bash (shell).

# List common binaries in /bin
ls /bin | grep -E 'ls|cp|mv|rm|bash'
# Output: bash  cp  ls  mv  rm

/boot (Boot Loader Files)

Stores files required to boot the system, including the Linux kernel (vmlinuz-*), initial RAM disk (initrd.img-*), and bootloader configuration (e.g., grub for GRUB2).

# List kernel and initrd files
ls /boot | grep -E 'vmlinuz|initrd'
# Output: initrd.img-5.15.0-78-generic  vmlinuz-5.15.0-78-generic

/dev (Device Files)

A virtual filesystem containing device nodes that represent hardware and pseudo-devices. These are not actual files but interfaces to interact with hardware (e.g., disks, keyboards) and system features (e.g., random number generators).

Examples:

  • /dev/sda: First SATA disk.
  • /dev/null: “Black hole” for discarding output.
  • /dev/zero: Generates null bytes.
# Read from /dev/zero (generates infinite nulls; Ctrl+C to stop)
head -c 10 /dev/zero | hexdump -C  # Output: 00000000  00 00 00 00 00 00 00 00  00 00  |..........|

/etc (System Configuration Files)

Contains system-wide configuration files and scripts. This is the primary directory for modifying system behavior.

Key Subdirectories/Examples:

  • /etc/passwd: User account information.
  • /etc/fstab: Filesystem mount configuration.
  • /etc/ssh/sshd_config: SSH server settings.
  • /etc/apache2/: Apache web server configs.
# View user accounts in /etc/passwd
cat /etc/passwd | head -3
# Output:
# root:x:0:0:root:/root:/bin/bash
# daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# bin:x:2:2:bin:/bin:/usr/sbin/nologin

/home (User Home Directories)

Each user gets a subdirectory here (e.g., /home/alice, /home/bob) to store personal files, settings, and user-specific application data.

# List user home directories
ls -l /home
# Output (example):
# drwxr-xr-x 10 alice alice 4096 Jun 1 12:00 alice
# drwxr-xr-x  8 bob   bob   4096 May 20 09:30 bob

/lib and /lib64 (Shared Libraries)

Contain shared library files (.so extensions) required by binaries in /bin and /sbin. /lib is for 32-bit libraries, and /lib64 for 64-bit (on 64-bit systems).

Example: libc.so.6 (GNU C library, critical for most binaries).

# Check which library a binary depends on
ldd /bin/ls | grep libc
# Output: libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8b...)

/media (Removable Media Mount Points)

Default mount point for removable media (e.g., USB drives, CDs). Automatically created by desktop environments (e.g., /media/user/USB-Drive).

/mnt (Temporary Mount Points)

Used by system administrators to manually mount filesystems (e.g., external hard drives, network shares). Unlike /media, it is not auto-managed by desktop tools.

# Mount a USB drive (e.g., /dev/sdb1) to /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
# Unmount when done
sudo umount /mnt/usb

/opt (Optional Application Software)

For third-party software not managed by the system package manager (e.g., commercial applications, self-compiled tools). Typically organized as /opt/<app-name>.

Example: /opt/google/chrome (Chrome browser installed manually).

/proc (Process Information Virtual Filesystem)

A virtual, in-memory filesystem that provides real-time information about running processes, system memory, and hardware. Files here are not stored on disk; they are generated dynamically by the kernel.

Examples:

  • /proc/cpuinfo: CPU details.
  • /proc/meminfo: Memory usage.
  • /proc/<PID>/cmdline: Command used to start process <PID>.
# View CPU model
grep 'model name' /proc/cpuinfo | head -1
# Output: model name	: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz

# View command for PID 1 (systemd on systemd-based distros)
cat /proc/1/cmdline | tr '\0' ' '; echo
# Output: /sbin/init splash

/root (Root User’s Home Directory)

Home directory for the root (administrative) user. Unlike regular users, root does not use /home/root; this ensures access even if /home is unmounted.

/run (Runtime Data)

Stores temporary runtime data (e.g., process IDs, socket files) that is recreated on each boot. Replaces older directories like /var/run (now a symlink to /run).

Example: /run/sshd.pid (PID of the SSH daemon).

/sbin (System Binaries)

Contains system administration binaries for tasks like mounting filesystems, managing users, and repairing the system. These are typically only accessible to the root user.

Examples: mount, umount, useradd, fsck (filesystem check).

/sys (Kernel Device Tree)

Another virtual filesystem that exposes kernel data structures, including device drivers, hardware status, and system configuration (e.g., CPU cores, GPU settings).

Example: /sys/class/net/enp0s3 (network interface settings).

/tmp (Temporary Files)

For temporary files created by users and applications. Files here may be deleted on reboot or by a cleanup daemon (e.g., systemd-tmpfiles). Accessible to all users.

Note: Never store critical data in /tmp!

/usr (User Utilities and Applications)

The largest directory, containing non-essential user binaries, libraries, documentation, and shared data. Often called the “Unix System Resources” directory.

Key Subdirectories:

  • /usr/bin: Non-essential user binaries (e.g., git, python3).
  • /usr/sbin: Non-essential system binaries (e.g., nginx, apache2).
  • /usr/lib: Libraries for /usr/bin and /usr/sbin.
  • /usr/share: Shared data (e.g., documentation, icons).
  • /usr/local: Locally installed software (e.g., /usr/local/bin for custom binaries).

/var (Variable Data)

Stores dynamic, frequently changing data generated by system services and applications.

Key Subdirectories:

  • /var/log: Log files (e.g., syslog, auth.log).
  • /var/www: Web server content (e.g., Apache/NGINX root).
  • /var/lib: Application state data (e.g., /var/lib/docker for Docker).
  • /var/spool: Queued data (e.g., print jobs in /var/spool/cups).
# Check size of log files (e.g., /var/log)
du -sh /var/log
# Output: 2.3G	/var/log

Practical Usage Methods

Use these commands to move between directories and inspect their contents:

CommandPurposeExample
pwdPrint current working directorypwd/home/user/Documents
cd <dir>Change directorycd /etc → Move to /etc
ls <dir>List directory contentsls -l /home → List user home dirs
tree <dir>Visualize directory structure (install with sudo apt install tree)tree -L 2 / → Show 2 levels of root

Monitoring Disk Usage

  • df -h: Check free space on mounted filesystems (human-readable units).

    df -h
    # Output (example):
    # Filesystem      Size  Used Avail Use% Mounted on
    # /dev/sda2       200G   50G  140G  27% /
    # /dev/sda1        512M   64M  448M  13% /boot
  • du -sh <dir>: Check size of a directory (summarize, human-readable).

    du -sh /var/lib/docker  # Size of Docker data
    # Output: 15G	/var/lib/docker

Mounting Filesystems

To access a storage device (e.g., USB drive, network share), mount it to a directory:

  1. Temporary Mount: Use mount for one-time access.

    # Identify the device (e.g., /dev/sdb1)
    lsblk
    # Mount to /mnt/usb
    sudo mount /dev/sdb1 /mnt/usb
  2. Permanent Mount: Add an entry to /etc/fstab to mount on boot.

    # Edit /etc/fstab (use sudo)
    sudo nano /etc/fstab
    # Add a line (example for USB drive with UUID):
    UUID=1234-ABCD  /mnt/usb  vfat  defaults,noatime  0  0

Common Practices

Organizing Files

  • User Data: Store personal files in /home/<user> (e.g., Documents, Downloads).
  • System Configuration: Modify files in /etc (e.g., /etc/ssh/sshd_config for SSH settings).
  • Third-Party Software: Install custom tools in /opt or /usr/local (avoids conflicts with package managers).

Symbolic links (symlinks) are shortcuts to files/directories, useful for organizing paths or versioning.

# Create a symlink (linkname → target)
ln -s /usr/local/bin/python3.9 /usr/bin/python  # Link python → python3.9

# Verify the symlink
ls -l /usr/bin/python
# Output: lrwxrwxrwx 1 root root 24 Jun 1 10:00 /usr/bin/python -> /usr/local/bin/python3.9

Mounting External Devices

  • Use /mnt for manual mounts (e.g., sudo mount /dev/sdc1 /mnt/backup).
  • Use /media for auto-mounted removable media (desktop environments handle this).

Best Practices for System Management

Permissions and Security

  • Least Privilege: Restrict file permissions with chmod and chown. For example, user data in /home should be rw------- (only accessible to the user).

    # Make a file readable/writable only by the owner
    chmod 600 /home/user/private.key
  • Avoid chmod 777: This grants read/write/execute to all users—never use it for system files or sensitive data!

Backup Critical Directories

Prioritize backups for directories containing irreplaceable data:

  • /etc: System configuration files.
  • /home: User data.
  • /var: Logs, databases, and application state.

Use tools like rsync or borgbackup for automated backups:

# Backup /etc to an external drive
sudo rsync -av /etc /mnt/backup/etc-backup-$(date +%Y%m%d)

Avoid Common Pitfalls

  • Don’t Fill / or /var: A full root filesystem can crash the system. Monitor with df -h and clean logs in /var/log if needed.
  • Avoid /tmp for Critical Data: Files here are deleted on reboot (or by tmpwatch). Use /var/tmp for longer-lived temporary files.
  • Don’t Modify /usr Manually: Use package managers (e.g., apt, dnf) to manage software in /usr; manual changes may break updates.

Conclusion

The Linux filesystem hierarchy is a structured, standardized framework that enables consistent system operation across distributions. By understanding directories like /etc (configuration), /home (user data), and /proc (process info), you gain the ability to troubleshoot issues, manage storage, and secure your system effectively.

Whether you’re a developer navigating $PATH or a sysadmin mounting network shares, mastery of this hierarchy is foundational to Linux proficiency. Refer to the FHS documentation and practice with commands like ls, cd, and df to deepen your familiarity.