_____                   _             ___     _________  __
 |_   _|__ _ __ _ __ ___ (_)_ __   __ _| \ \   / / ____\ \/ /
   | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | |\ \ / /|  _|  \  /
   | |  __/ |  | | | | | | | | | | (_| | | \ V / | |___ /  \
   |_|\___|_|  |_| |_| |_|_|_| |_|\__,_|_|  \_/  |_____/_/\_\
basics10 min

Linux File System: Understanding the Directory Structure

The Linux File System Hierarchy

In the Linux operating system, everything is a file. This statement summarizes the fundamental philosophy of Linux. Your documents, programs, and even hardware devices are all represented as files or directories within the file system. Unlike Windows, Linux does not use drive letters like C: or D:. Instead, it uses a tree-like hierarchy starting from a single root directory.

This hierarchy is defined by a standard called the Filesystem Hierarchy Standard (FHS). The FHS specifies which directory is used for what purpose, ensuring consistency across Linux distributions. Thanks to this standard, the directory structure you learn on Ubuntu also applies to Fedora, Debian, or Arch Linux.

The Root Directory: / (Root)

At the very top of the Linux file system sits the root directory, represented by a single forward slash (/). All files and directories branch out from this point. The root directory is like the trunk of the tree; all other directories are its branches. Only the root user (superuser) can write directly to the root directory.

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

/home — User Home Directories

The /home directory contains a separate subdirectory for each regular user on the system. For example, a user named "ali" would have a home directory at /home/ali. Personal documents, downloads, configuration files, and desktop content are all stored here.

$ ls /home/ali
Documents  Downloads  Desktop  Music  Pictures  .bashrc  .config

You can use the tilde (~) symbol as a shortcut to your home directory. Typing cd ~ or just cd takes you to your home directory. It is also accessible as the $HOME environment variable.

/etc — System Configuration Files

The /etc directory houses the system's configuration files. Its name comes from "et cetera" (and so on), though it is also interpreted as "Editable Text Configuration" today. Network settings, user account information, service configurations, and security policies are all kept here.

$ ls /etc
apache2  bash.bashrc  crontab  fstab  hostname  hosts  network  passwd  shadow  ssh  sudoers

Some important configuration files include:

  • /etc/passwd: Contains user account information.
  • /etc/shadow: Stores encrypted user passwords.
  • /etc/hosts: Holds domain name-to-IP mappings for local DNS resolution.
  • /etc/fstab: Defines disk partitions to be automatically mounted at system startup.
  • /etc/ssh/sshd_config: Contains SSH server configuration.

Editing these files requires root privileges. An incorrect edit can render the system unbootable, so always create a backup before making changes.

/var — Variable Data

The /var directory holds data whose size changes continuously. Its name comes from "variable." Log files, email queues, database files, and web server content are stored here.

$ ls /var
backups  cache  lib  lock  log  mail  opt  run  spool  tmp  www
  • /var/log: System and application log files. This is the most frequently consulted directory for troubleshooting. Files like syslog, auth.log, and kern.log reside here.
  • /var/www: Default content directory for web servers like Apache and Nginx.
  • /var/cache: Application cache files are stored here.
  • /var/lib: Directory where programs store runtime state information. Database files are typically located here.

/tmp — Temporary Files

The /tmp directory holds temporary files created by programs. Files in this directory are usually deleted automatically when the system reboots. All users can write to this directory, but for security the sticky bit is set, meaning each user can only delete files they created themselves.

$ ls -ld /tmp
drwxrwxrwt 15 root root 4096 Jan 10 14:30 /tmp

The "t" at the end of the permissions indicates the sticky bit.

/usr — User Programs and Data

The /usr directory stands for "Unix System Resources" and contains the vast majority of user-installed programs. Programs beyond the essential tools that ship with the operating system are typically installed here.

$ ls /usr
bin  include  lib  lib64  local  sbin  share  src
  • /usr/bin: User commands and programs (python, gcc, vim, git, etc.).
  • /usr/sbin: System administration programs.
  • /usr/lib: Library files used by programs.
  • /usr/local: A special directory reserved for programs manually installed by the system administrator. Software compiled outside the package manager is installed here.
  • /usr/share: Architecture-independent shared data: documentation, icons, fonts, etc.

/bin and /sbin — Essential System Commands

The /bin directory contains commands essential for the system to function: ls, cp, mv, rm, cat, echo, and so on. These commands must be accessible even in single-user mode.

The /sbin directory holds commands needed for system administration: fdisk, mkfs, iptables, reboot, etc. These commands typically require root privileges.

$ ls /bin
bash  cat  cp  date  echo  grep  ls  mkdir  mv  pwd  rm  sh  touch

$ ls /sbin
fdisk  fsck  halt  ifconfig  init  iptables  mkfs  reboot  shutdown

In modern Linux distributions, /bin and /sbin are usually symbolic links to /usr/bin and /usr/sbin.

/opt — Optional Software

The /opt directory is reserved for third-party software installations. Applications like Google Chrome, Visual Studio Code, and Zoom are typically installed here. Each application gets its own subdirectory: /opt/google/chrome, /opt/vscode, and so on.

/dev — Device Files

The /dev directory contains special files that represent hardware devices. It is the most concrete example of the Linux philosophy that "everything is a file." Hard drives, USB drives, terminals, and even random number generators exist here as files.

$ ls /dev
sda  sda1  sda2  sdb  tty  tty0  null  zero  random  urandom
  • /dev/sda: The first hard disk.
  • /dev/sda1: The first partition of the first hard disk.
  • /dev/null: The "black hole" — everything written here disappears. Used to discard unwanted output.
  • /dev/zero: A virtual device that produces zero bytes.
  • /dev/random: A device that generates random data, used in cryptographic operations.

/proc — Process and Kernel Information

The /proc directory is a virtual file system; it does not physically occupy space on disk. It provides real-time information about running processes, CPU details, memory status, and kernel parameters.

$ cat /proc/cpuinfo    # CPU information
$ cat /proc/meminfo    # Memory usage
$ ls /proc/1234        # Information about process with PID 1234
$ cat /proc/version    # Kernel version

Absolute and Relative Paths

In Linux, you can refer to a file or directory in two ways:

Absolute Path

A complete path starting from the root directory (/). It always points to the same location, regardless of your current directory.

$ cat /home/ali/Documents/notes.txt
$ cd /var/log

Relative Path

A path defined relative to your current directory. . represents the current directory, and .. represents the parent directory.

$ cd Documents          # Go to the Documents folder in the current directory
$ cat ./notes.txt       # Read a file in the current directory
$ cd ../Pictures        # Go up one level, then into Pictures

Navigating Between Directories

Once you understand the file system structure, here are some practical methods for navigating efficiently:

$ cd -                 # Return to the previous directory
$ pushd /var/log       # Push current directory to stack and go to /var/log
$ popd                 # Return to the last directory on the stack
$ find / -name "*.conf" -type f  # Find all .conf files

Understanding the Linux file system structure is the foundation for working effectively on the command line. Each directory has a specific purpose, and knowing these purposes tells you where to look for your files and where to edit system configurations.