Day 1: Linux File System Hierarchy Explained

Md YunusMd Yunus
4 min read

The Linux file system follows the Filesystem Hierarchy Standard (FHS), a well-defined structure for organizing directories and files. This hierarchy ensures consistency across distributions and simplifies system management. Let’s break down the key directories, their purposes, and real-world use cases.


1. Root Directory (/)

The root directory (/) is the top-level directory from which all other directories branch out. Think of it as the "trunk" of the filesystem tree.


2. Essential System Directories

  1. /bin (Binary)

    • Purpose: Contains essential user command binaries (executable programs) needed for basic system functionality.

    • Examples: ls, cp, mv, bash, cat.

    • Key Point: Accessible to all users (even in single-user/rescue mode).

  2. /sbin (System Binary)

    • Purpose: Holds system administration binaries used for system maintenance and repair.

    • Examples: fdisk (partition tool), iptables (firewall), reboot.

    • Key Point: Typically requires root/sudo privileges to run.

  3. /etc (Editable Text Configuration)

    • Purpose: Stores system-wide configuration files and scripts.

    • Examples:

      • /etc/passwd: User account information.

      • /etc/fstab: Filesystem mount points.

      • /etc/nginx/: Configuration for the Nginx web server.

    • Key Point: Never delete/modify files here without understanding their purpose!

  4. /var (Variable Data)

    • Purpose: Contains variable data that changes during system operation.

    • Subdirectories:

      • /var/log: System and application logs (e.g., syslog, nginx/access.log).

      • /var/cache: Temporary data for applications (e.g., package manager cache).

      • /var/www: Default directory for web server content (e.g., Apache/Nginx).

      • /var/lib: Databases and state files (e.g., Docker containers, MySQL data).

    • Key Point: Critical for debugging (check logs!) and managing dynamic data.


3. User & Home Directories

  1. /home

    • Purpose: Personal directories for non-root users (e.g., /home/alice).

    • Key Point: Users store personal files, configurations (dotfiles like .bashrc), and projects here.

  2. /root

    • Purpose: Home directory for the root user (system administrator).

    • Key Point: Separate from /home for security reasons.


4. System Data & Devices

  1. /dev (Devices)

    • Purpose: Contains device files representing hardware or virtual devices.

    • Examples:

      • /dev/sda: First SATA disk.

      • /dev/null: A "black hole" that discards data.

      • /dev/tty: Represents a terminal.

  2. /proc (Processes)

    • Purpose: Virtual filesystem providing real-time system/process information.

    • Examples:

      • /proc/cpuinfo: CPU details.

      • /proc/meminfo: Memory usage.

      • /proc/<PID>/: Directories for running processes (e.g., PID 1 is systemd).

  3. /sys (System)

    • Purpose: Virtual filesystem for kernel and hardware configuration (e.g., power management, device drivers).

5. Temporary & Runtime Data

  1. /tmp (Temporary)

    • Purpose: Stores temporary files deleted on reboot.

    • Key Point: All users can write here, but files are not persistent.

  2. /run (Runtime)

    • Purpose: Holds volatile runtime data (e.g., PID files, sockets) created after boot.

    • Example: /run/docker.sock (Docker daemon socket).


6. Application & Software Directories

  1. /usr (Unix System Resources)

    • Purpose: Contains read-only user binaries, libraries, and documentation.

    • Subdirectories:

      • /usr/bin: Non-essential user binaries (e.g., python, git).

      • /usr/sbin: Non-essential system admin binaries.

      • /usr/lib: Shared libraries for /usr/bin and /usr/sbin.

      • /usr/local: Locally compiled software (e.g., manually installed apps).

  2. /opt (Optional)

    • Purpose: Stores third-party/standalone software (e.g., proprietary apps like Google Chrome).
  3. /lib (Libraries)

    • Purpose: Shared libraries for binaries in /bin and /sbin (e.g., libc.so for the C library).

7. Boot & Mount Points

  1. /boot

    • Purpose: Contains bootloader files (e.g., GRUB) and the Linux kernel (vmlinuz).
  2. /mnt (Mount) & /media

    • Purpose: Temporary mount points for external devices (e.g., USB drives, network shares).

    • Difference:

      • /mnt: Manually mounted devices.

      • /media: Automatically mounted removable media (e.g., by the desktop environment).


8. Key Takeaways

  • Critical Directories:

    • /etc: Configuration files.

    • /var: Logs, databases, and dynamic data.

    • /bin & /sbin: Essential binaries.

  • User Data:

    • /home: User files.

    • /root: Admin’s home.

  • Temporary Data:

    • /tmp: Non-persistent files.

    • /run: Runtime data.

  • Never Delete:

    • /bin, /sbin, /etc, /lib (breaking these can crash your system).

9. Real-World Example

Imagine deploying a web app:

  • Store code in /var/www/myapp.

  • Configure Nginx in /etc/nginx/nginx.conf.

  • Logs go to /var/log/nginx/access.log.

  • Use /tmp for temporary uploads.


10. Next Steps

  • Explore your system with ls, cd, and tree.

  • Run man hier for the official manual on the filesystem hierarchy.

  • Check disk usage with du -sh /var/log (replace with any directory).

Understanding the Linux file system is foundational for troubleshooting, scripting, and mastering DevOps tools like Docker, Kubernetes, and configuration management (Ansible, Terraform). 🐧

0
Subscribe to my newsletter

Read articles from Md Yunus directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Yunus
Md Yunus