OS: Linux Filesystem Structure

Linux organizes files in a single hierarchical directory structure, starting from the root directory /. Unlike Windows, there are no drive letters everything is placed under /.

Linux Directory Tree

Understanding the purpose of the main directories is essential for navigation, troubleshooting, and system administration.

Visual Overview

bash
/
├── bin   → Essential user commands (ls, cp, mv)
├── boot  → Boot loader files, Linux kernel, initrd
├── dev   → Device files (disks, USB, tty)
├── etc   → System configuration files
├── home  → User home directories (/home/username)
├── lib   → Essential shared libraries for /bin and /sbin
├── media → Mount points for removable devices (USB, CD)
├── mnt   → Temporary mount point for filesystems
├── opt   → Optional add-on software packages
├── proc  → Virtual filesystem for processes & kernel info
├── root  → Home directory for the root (superuser) account
├── run   → Runtime variable data (PID files, sockets)
├── sbin  → Essential system binaries (fdisk, ifconfig)
├── srv   → Data for system services (e.g., web, FTP)
├── sys   → Virtual filesystem for hardware & kernel
├── tmp   → Temporary files (cleared at reboot)
├── usr   → User programs, libraries, docs
│   ├── bin   → Non-essential user binaries
│   ├── lib   → Libraries for /usr/bin and /usr/sbin
│   ├── local → Locally installed software
│   └── share → Architecture-independent data
└── var   → Variable data (logs, mail, caches)
    ├── log → Log files
    ├── cache → Cached data
    └── spool → Print/mail spools

 `/bin` – Essential User Binaries

  • Contains basic commands needed by all users, even in single-user or recovery mode.
  • Examples: lscpmvcatmkdir.
  • Always available, even if other parts of the system are not mounted.

 `/etc` – System Configuration Files

  • Stores system-wide configuration files and startup scripts.
  • Examples:
  • bash
    /etc     
    ├── /passwd/           # user account information
    ├── /fstab/            # shared libraries.
    ├── /ssh/sshd_config/  # documentation, icons, default configs.

    Almost everything you configure in Linux happens through files here.


 `/home` – User Home Directories

  • Each user has a personal workspace under /home/username.
  • Stores documents, downloads, configurations, and personal scripts.
  • Example: /home/amr/Documents/notes.txt.
  • Equivalent to C:\Users\ in Windows.

 `/var` – Variable Data Files

  • Contains data that changes frequently while the system is running.
  • Examples:
  • /var/log/ → system and application logs.
  • /var/spool/ → print queues, mail spools.
  • /var/cache/ → cached files.
  • Useful when troubleshooting — most logs live here.

 `/usr` – User System Resources

  • Stores user applications, libraries, and documentation.
  • Subdirectories include:
  • /usr/bin/ → non-essential user binaries (installed programs).
  • /usr/lib/ → shared libraries.
  • /usr/share/ → documentation, icons, default configs.
  • Equivalent to “Program Files” in Windows, but better organized.

In summary:

  • / → the root of everything.
  • /bin/sbin/lib → core system utilities and libraries.
  • /etc → the brain (configurations).
  • /home → users’ personal space.
  • /var → logs and dynamic data.
  • /usr → applications and shared resources.
  • Virtual filesystems like /proc and /sys don’t contain “real” files but expose system and kernel info.

Absolute vs Relative Paths

In Linux, every file and directory has a path — a unique location in the filesystem. When you use commands like cdcp, or ls, you need to specify paths. There are two main types: absolute and relative.

Absolute Path

An absolute path starts from the root directory / and describes the full location of a file or directory.

  • Always begins with /.
  • Works from _anywhere_ in the system.

Example:

bash
cd /home/amr/Documents

This command takes you to the Documents folder inside user _amr_’s home directory, no matter where you currently are.


Relative Path

relative path describes a location relative to your current working directory.

  • Does not start with /.
  • Depends on where you are right now (pwd).
  • Example:

If you are in /home/amr:

cd Documents

takes you to /home/amr/Documents.


📌 Special Notations

  • . → current directory
  • bash
        cd .

    (stays in the same place)

  • .. → parent directory
  • bash
    cd ..

    (goes one level up)

  • ~ → home directory of the current user
  • bash
    cd ~

  • (goes directly to /home/username)

Quick Comparison

TypeExampleMeaning
Absolute Path/bin/testAlways points to the same file
Relative Path../Documents/notes.txtDepends on where you are (pwd)

In short:

  • Use absolute paths for precision and scripts.
  • Use relative paths for convenience when working interactively.

Mounting Drives and Partitions

In Linux, drives and partitions are not automatically assigned drive letters (like C: or D: in Windows). Instead, they are mounted into the single directory tree. Mounting means making the contents of a storage device (partition, USB, CD, network share) accessible under a specific directory.


Key Concepts

  • Device files → Stored in /dev/ (e.g., /dev/sda1/dev/sdb).
  • sda = first disk, sdb = second disk.
  • Numbers (sda1sda2) = partitions.
  • Mount point → A directory where the device is attached.
  • Example: /mnt/usb or /media/username/USB_DRIVE.
  • Unmounting → Safely detaching the device so data isn’t lost.

Mounting a Drive Manually

  1. Create a mount point:
  2. bash
    sudo mkdir /mnt/usb

  1. Mount the device:
  2. bash
    sudo mount /dev/sdb1 /mnt/usb

  1. Access files:
  2. bash
    ls /mnt/usb

  1. Unmount when done:
  2. bash
    sudo umount /mnt/usb


⚙️ Viewing Mounted Drives

  • Show all mounted filesystems:
  • bash
    mount

  • Or use the modern command:
  • bash
    lsblk

  • Check disk usage:
  • bash
    df -h


🛠️ Auto-Mount at Boot

To mount a drive automatically at startup, you edit / etc/fstab. Example entry:

bash
/dev/sdb1 /mnt/data ext4 defaults 0 2

  • /dev/sdb1 → the partition
  • /mnt/data → mount point
  • ext4 → filesystem type
  • defaults → options
  • 0 2 → backup and check settings
  • Be careful: mistakes in / etc/fstab can prevent Linux from booting.

Summary

  • mount → attach a drive to the filesystem.
  • umount → detach safely.
  • lsblk / df -h → check drives and usage.
  • / etc/fstab → configure permanent mounts.

Disk Usage & Monitoring (df, du, free, top)

Monitoring disk and memory usage is crucial for system performance, troubleshooting, and ensuring you don’t run out of resources. Linux provides several built-in commands for this.


 `df` – Disk Free Space

Shows the available and used disk space on mounted filesystems.

  • Basic usage:
  • bash
    df

  • Human-readable format:
  • bash
    df -h

Example output:

bash
Filesystem   Size  Used Avail Use% Mounted on
/dev/sda1     50G   20G   28G  42% /
tmpfs        2.0G  2.0M  2.0G   1% /run


`du` – Disk Usage per Directory

Shows how much space directories and files consume.

  • Check size of current folder:
  • bash
    du -sh .

  • Check size of subdirectories:
  • bash
    du -sh *

  • Useful for finding large files/folders.

`free` – Memory Usage

Displays memory (RAM and swap) usage.

  • Basic usage:
  • bash
    free

  • Human-readable format:
  • bash
    free -h

Example:

bash
            total   used   free   shared  buff/cache  available
Mem:          16G     5G    7G      1G         4G        10G
Swap:          2G     0B    2G


 `top` – Live Resource Monitor

Shows real-time CPU, memory, and process usage.

  • Run:
  • bash
    top

  • Press inside top:
  • q → quit
  • M → sort by memory usage
  • P → sort by CPU usage
  • Alternative: htop (more user-friendly, if installed).

Summary

  • df -h → check overall disk usage.
  • *du -sh ** → check folder sizes.
  • free -h → check memory usage.
  • top / htop → monitor live system performance.

With these tools, you can easily find space hogs, monitor memory leaks, and keep your system healthy.