Skip to content

Latest commit

Β 

History

History
404 lines (309 loc) Β· 11.6 KB

File metadata and controls

404 lines (309 loc) Β· 11.6 KB

πŸ“ Chapter 04: Filesystem Hierarchy

Beginner Chapter 04


πŸ“‘ Table of Contents


Everything is a File

One of the most important Linux philosophies:

"On a Linux system, everything is a file. If something is not a file, it is a process."

This means:

  • Regular files β†’ files
  • Directories β†’ special files
  • Hard drives β†’ files (/dev/sda)
  • USB devices β†’ files (/dev/usb)
  • Network sockets β†’ files
  • Running processes β†’ files (in /proc)
  • Even your keyboard and screen β†’ files (/dev/stdin, /dev/stdout)

The FHS (Filesystem Hierarchy Standard)

The FHS defines the standard directory structure for Linux systems.

/                       ← Root of everything
β”œβ”€β”€ bin/                ← Essential user binaries
β”œβ”€β”€ boot/               ← Boot loader files, kernel
β”œβ”€β”€ dev/                ← Device files
β”œβ”€β”€ etc/                ← System configuration files
β”œβ”€β”€ home/               ← User home directories
β”‚   β”œβ”€β”€ sovon/
β”‚   └── alice/
β”œβ”€β”€ lib/                ← Shared libraries
β”œβ”€β”€ lib64/              ← 64-bit shared libraries
β”œβ”€β”€ media/              ← Mount points for removable media
β”œβ”€β”€ mnt/                ← Temporary mount points
β”œβ”€β”€ opt/                ← Optional/third-party software
β”œβ”€β”€ proc/               ← Virtual filesystem for processes
β”œβ”€β”€ root/               ← Root user's home directory
β”œβ”€β”€ run/                ← Runtime data
β”œβ”€β”€ sbin/               ← System binaries (admin commands)
β”œβ”€β”€ srv/                ← Service data
β”œβ”€β”€ sys/                ← Virtual filesystem for hardware
β”œβ”€β”€ tmp/                ← Temporary files (cleared on reboot)
β”œβ”€β”€ usr/                ← User programs & data (read-only)
β”‚   β”œβ”€β”€ bin/
β”‚   β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ local/
β”‚   └── share/
└── var/                ← Variable data (logs, mail, spool)
    β”œβ”€β”€ log/
    β”œβ”€β”€ cache/
    └── tmp/

Root Directory /

The / (slash) is the top-level directory. Everything in Linux exists under /.

🏠 Analogy: If your Linux system were a building, / would be the ground floor lobby. Every room (directory) and every item (file) in the building is accessed from here.

⚠️ Warning: / (root directory) β‰  /root (root user's home directory). Don't confuse them!


Directory Deep Dive

/bin β€” Essential User Binaries

Contains commands that all users need, even in single-user mode.

ls /bin
# Contains: ls, cp, mv, rm, cat, grep, bash, echo, mount ...

πŸ“ On modern systems, /bin is often a symlink to /usr/bin.


/boot β€” Boot Files

Contains everything needed to start the system.

ls /boot
# vmlinuz-6.8.0-xx    ← The Linux kernel
# initrd.img-6.8.0-xx ← Initial ramdisk
# grub/               ← GRUB bootloader config

⚠️ Do NOT delete files in /boot unless you know exactly what you're doing.


/dev β€” Device Files

Everything is a file β€” even hardware devices.

ls /dev
# sda         ← First hard drive
# sda1        ← First partition of sda
# sdb         ← Second hard drive (or USB)
# null        ← Black hole (discards all data)
# zero        ← Infinite stream of zeros
# random      ← Random number generator
# urandom     ← Faster random generator
# tty         ← Terminal devices
# stdin       ← Standard input (keyboard)
# stdout      ← Standard output (screen)
# stderr      ← Standard error (screen)
# Useful /dev tricks
echo "Gone forever" > /dev/null     # Discard output
dd if=/dev/zero bs=1M count=100 of=testfile  # Create a 100MB file of zeros
cat /dev/urandom | head -c 32 | base64       # Generate random string

/etc β€” Configuration Files

All system-wide configuration lives here. This is one of the most important directories.

ls /etc
# passwd       ← User account information
# shadow       ← Encrypted passwords
# group        ← Group information
# hostname     ← System hostname
# hosts        ← Static hostname to IP mapping
# fstab        ← Filesystem mount table
# sudoers      ← Sudo permissions
# ssh/         ← SSH configuration
# apt/         ← APT package manager config
# cron.d/      ← Cron job configurations
# systemd/     ← Systemd configuration
# nginx/       ← Nginx web server config (if installed)
# Peek at common config files
cat /etc/hostname       # Your computer's name
cat /etc/os-release     # Distro information
head -5 /etc/passwd     # First 5 user accounts

πŸ“ "etc" originally stood for "et cetera" but is now treated as "Editable Text Configuration."


/home β€” User Home Directories

Each user gets their own directory under /home.

/home/
β”œβ”€β”€ sovon/          ← Your stuff
β”‚   β”œβ”€β”€ Desktop/
β”‚   β”œβ”€β”€ Documents/
β”‚   β”œβ”€β”€ Downloads/
β”‚   β”œβ”€β”€ .bashrc     ← Bash configuration (hidden)
β”‚   β”œβ”€β”€ .ssh/       ← SSH keys (hidden)
β”‚   └── .config/    ← App settings (hidden)
└── alice/          ← Another user's stuff
# The ~ shortcut always refers to YOUR home directory
echo ~              # /home/sovon
echo ~alice         # /home/alice

# Important dotfiles in your home
cat ~/.bashrc       # Bash shell configuration
cat ~/.profile      # Login shell configuration
ls ~/.ssh/          # SSH keys and config

/proc β€” Process Information (Virtual)

A virtual filesystem β€” files here don't exist on disk. The kernel generates them on-the-fly.

ls /proc
# 1/         ← Process with PID 1 (systemd/init)
# 1234/      ← Process with PID 1234
# cpuinfo    ← CPU information
# meminfo    ← Memory information
# version    ← Kernel version
# uptime     ← System uptime
# loadavg    ← System load averages

# Useful examples
cat /proc/cpuinfo       # Detailed CPU info
cat /proc/meminfo       # Memory details
cat /proc/version       # Kernel version string
cat /proc/uptime        # Uptime in seconds
cat /proc/loadavg       # Load averages
cat /proc/partitions    # Disk partitions

/var β€” Variable Data

Data that changes frequently during normal operation.

/var/
β”œβ”€β”€ log/        ← System logs
β”‚   β”œβ”€β”€ syslog
β”‚   β”œβ”€β”€ auth.log
β”‚   β”œβ”€β”€ kern.log
β”‚   └── apt/
β”œβ”€β”€ cache/      ← Application cache
β”‚   └── apt/    ← Downloaded .deb packages
β”œβ”€β”€ mail/       ← User mail
β”œβ”€β”€ spool/      ← Print and mail queues
β”œβ”€β”€ tmp/        ← Temporary files (survives reboot)
└── www/        ← Web server files (Apache/Nginx)
# Check recent system logs
sudo tail -20 /var/log/syslog

# See authentication logs
sudo tail -20 /var/log/auth.log

# Check disk usage of /var
du -sh /var/*

/tmp β€” Temporary Files

# Anyone can write here. Files are deleted on reboot.
echo "temporary data" > /tmp/mytemp.txt
ls -la /tmp

# Check the sticky bit (the 't' at the end)
ls -ld /tmp
# drwxrwxrwt  ← 't' means only the owner can delete their own files

/usr β€” User Programs

Contains the majority of user-facing programs and data. Treated as read-only.

/usr/
β”œβ”€β”€ bin/        ← Most user commands (ls, grep, vim, python, etc.)
β”œβ”€β”€ sbin/       ← Non-essential system commands
β”œβ”€β”€ lib/        ← Libraries for /usr/bin and /usr/sbin
β”œβ”€β”€ local/      ← Locally installed software (compiled from source)
β”‚   β”œβ”€β”€ bin/
β”‚   β”œβ”€β”€ lib/
β”‚   └── share/
β”œβ”€β”€ share/      ← Architecture-independent data (man pages, icons)
β”‚   β”œβ”€β”€ man/    ← Manual pages
β”‚   β”œβ”€β”€ doc/    ← Documentation
β”‚   └── icons/  ← System icons
└── include/    ← C/C++ header files

/opt β€” Optional Software

Third-party software that doesn't follow the standard /usr layout.

/opt/
β”œβ”€β”€ google/chrome/      ← Google Chrome
β”œβ”€β”€ discord/            ← Discord
└── lampp/              ← XAMPP web stack

Special Filesystems

/proc and /sys β€” Virtual Filesystems

These don't exist on disk β€” the kernel creates them in memory.

Filesystem Purpose Example
/proc Process and kernel info /proc/cpuinfo, /proc/1/status
/sys Hardware and driver info /sys/class/net/, /sys/block/sda/
# Find your network interfaces
ls /sys/class/net/

# Check battery status (laptops)
cat /sys/class/power_supply/BAT0/capacity

# Check disk scheduler
cat /sys/block/sda/queue/scheduler

File Types in Linux

Linux has 7 file types, identified by the first character in ls -l output:

Symbol Type Description
- Regular file Text files, binaries, images
d Directory Contains other files
l Symbolic link Pointer to another file
c Character device Serial data (keyboard, terminal)
b Block device Block data (hard drives)
s Socket Inter-process communication
p Named pipe (FIFO) Inter-process communication
# Identify file type
file /bin/bash          # ELF 64-bit executable
file /etc/passwd        # ASCII text
file /dev/sda           # block special

# Find all symlinks in /usr/bin
find /usr/bin -type l | head -10

Absolute vs Relative Paths

Type Starts With Example Meaning
Absolute / /home/sovon/Documents Full path from root
Relative Anything else Documents or ./Documents Relative to current directory

Special Path Symbols

Symbol Meaning
/ Root directory
~ Home directory
. Current directory
.. Parent directory
- Previous directory (for cd)
# These are equivalent (if you're in /home/sovon):
cd /home/sovon/Documents    # Absolute
cd ~/Documents              # Using ~
cd Documents                # Relative
cd ./Documents              # Relative (explicit)

πŸ‹οΈ Practice Exercises

  1. Explore: Run ls / and identify each directory's purpose
  2. Config Files: Read /etc/os-release β€” what distro are you running?
  3. Processes: Run ls /proc and find your shell's PID with echo $$, then ls /proc/$$
  4. Hardware: Read /proc/cpuinfo β€” how many CPU cores do you have?
  5. Memory: Read /proc/meminfo β€” how much total RAM do you have?
  6. Devices: List /dev β€” can you find your hard drive device?
  7. Logs: Run sudo tail -10 /var/log/syslog β€” what's the latest log entry?
  8. File Types: Run file /bin/bash /etc/passwd /dev/null β€” what types are they?

← Previous: Terminal Basics Β· 🏠 Home Β· Next: File & Directory Operations β†’