Skip to content

Latest commit

Β 

History

History
321 lines (258 loc) Β· 11.7 KB

File metadata and controls

321 lines (258 loc) Β· 11.7 KB

02 β€” File System Structure and Navigation

← OS Fundamentals | Index | Linux CLI β†’


What is a File System?

A file system is the method and data structure that an OS uses to control how data is stored and retrieved on storage devices. Without a file system, data on disk would be one large unstructured blob.

File System Responsibilities

  • Organizing data into files and directories
  • Tracking free/used disk space
  • Managing file metadata (name, size, timestamps, permissions)
  • Providing access control
  • Supporting operations: create, read, write, delete, rename

Common File System Types

File System OS Max File Size Max Volume Features
ext4 Linux 16 TB 1 EB Journaling, widely used
btrfs Linux 16 EB 16 EB Snapshots, RAID, CoW
xfs Linux 8 EB 8 EB High performance, large files
zfs Linux/FreeBSD 16 EB 256 ZB Pooled storage, checksums
NTFS Windows 16 TB 256 TB ACLs, encryption, journaling
FAT32 Universal 4 GB 8 TB Legacy, USB drives
exFAT Universal 128 PB 128 PB Modern USB/SD cards
APFS macOS 8 EB 8 EB Encryption, snapshots

Linux File System Hierarchy (FHS)

The Filesystem Hierarchy Standard (FHS) defines the directory structure for Linux.

/                           ← Root (everything lives here)
β”œβ”€β”€ bin/                    ← Essential user binaries (ls, cp, bash)
β”œβ”€β”€ sbin/                   ← System binaries (for root: fdisk, iptables)
β”œβ”€β”€ boot/                   ← Bootloader files, kernel images
β”œβ”€β”€ dev/                    ← Device files (sda, tty, null, random)
β”œβ”€β”€ etc/                    ← System-wide configuration files
β”‚   β”œβ”€β”€ passwd              ← User account info
β”‚   β”œβ”€β”€ shadow              ← Hashed passwords
β”‚   β”œβ”€β”€ hosts               ← Static hostname/IP map
β”‚   β”œβ”€β”€ fstab               ← Filesystem mount table
β”‚   β”œβ”€β”€ ssh/                ← SSH daemon configuration
β”‚   └── systemd/            ← Systemd unit files
β”œβ”€β”€ home/                   ← User home directories
β”‚   β”œβ”€β”€ alice/              ← Alice's files
β”‚   └── bob/                ← Bob's files
β”œβ”€β”€ lib/                    ← Shared libraries for /bin and /sbin
β”œβ”€β”€ lib64/                  ← 64-bit shared libraries
β”œβ”€β”€ media/                  ← Mount points for removable media
β”œβ”€β”€ mnt/                    ← Temporary mount points
β”œβ”€β”€ opt/                    ← Optional/third-party software
β”œβ”€β”€ proc/                   ← Virtual FS β€” kernel/process info
β”‚   β”œβ”€β”€ cpuinfo             ← CPU details
β”‚   β”œβ”€β”€ meminfo             ← Memory details
β”‚   └── [PID]/              ← Per-process info
β”œβ”€β”€ root/                   ← Root user's home directory
β”œβ”€β”€ run/                    ← Runtime data (PIDs, sockets)
β”œβ”€β”€ srv/                    ← Data served by the system (web, FTP)
β”œβ”€β”€ sys/                    ← Virtual FS β€” hardware/kernel info
β”œβ”€β”€ tmp/                    ← Temporary files (cleared on reboot)
β”œβ”€β”€ usr/                    ← User utilities and applications
β”‚   β”œβ”€β”€ bin/                ← Most user commands (git, python, vim)
β”‚   β”œβ”€β”€ lib/                ← Libraries for /usr/bin
β”‚   β”œβ”€β”€ local/              ← Locally compiled software
β”‚   └── share/              ← Architecture-independent data
└── var/                    ← Variable data (logs, spools, caches)
    β”œβ”€β”€ log/                ← System logs
    β”œβ”€β”€ www/                ← Web server files
    β”œβ”€β”€ cache/              ← Application cache data
    └── run/                ← Runtime variable data

Key Directories Explained

Directory Purpose Example Contents
/etc System configuration /etc/nginx/nginx.conf, /etc/hosts
/var/log Log files /var/log/syslog, /var/log/auth.log
/proc Virtual: process/kernel info /proc/cpuinfo, /proc/1234/maps
/sys Virtual: hardware/driver info /sys/class/net/eth0/
/dev Device files /dev/sda, /dev/null, /dev/tty
/tmp Temporary files Cleared on boot
/home User data /home/username/
/root Root user home /root/.ssh/authorized_keys

Windows File System Structure

Windows uses drive letters as roots (C:, D:, etc.), unlike Linux's unified /.

C:\                             ← System drive (primary)
β”œβ”€β”€ Windows\                    ← OS files
β”‚   β”œβ”€β”€ System32\               ← Core system DLLs and executables
β”‚   β”œβ”€β”€ SysWOW64\               ← 32-bit compat on 64-bit
β”‚   β”œβ”€β”€ drivers\                ← Device drivers (.sys files)
β”‚   └── Temp\                   ← System temp files
β”œβ”€β”€ Program Files\              ← 64-bit installed applications
β”œβ”€β”€ Program Files (x86)\        ← 32-bit installed applications
β”œβ”€β”€ Users\                      ← User profiles
β”‚   β”œβ”€β”€ Username\
β”‚   β”‚   β”œβ”€β”€ Desktop\
β”‚   β”‚   β”œβ”€β”€ Documents\
β”‚   β”‚   β”œβ”€β”€ Downloads\
β”‚   β”‚   β”œβ”€β”€ AppData\
β”‚   β”‚   β”‚   β”œβ”€β”€ Local\          ← Local app data
β”‚   β”‚   β”‚   β”œβ”€β”€ LocalLow\       ← Low-integrity app data
β”‚   β”‚   β”‚   └── Roaming\        ← Syncs with domain profile
β”‚   β”‚   └── .ssh\               ← SSH keys
β”‚   └── Public\                 ← Shared between all users
β”œβ”€β”€ ProgramData\                ← Machine-wide app data (hidden)
└── Temp\                       ← System temp (also %TEMP%)

Windows Special Paths

Path Variable Purpose
C:\Users\%USERNAME% %USERPROFILE% Current user's home
C:\Windows\System32 %WINDIR%\System32 System executables
C:\Users\%USERNAME%\AppData\Roaming %APPDATA% App config (roaming)
C:\Users\%USERNAME%\AppData\Local %LOCALAPPDATA% App data (local)
C:\ProgramData %PROGRAMDATA% Machine-wide app data
C:\Windows\Temp %TEMP% Temp files

Absolute vs Relative Paths

Linux Examples

Absolute: /home/alice/documents/report.pdf
           ↑ starts from root /

Relative: documents/report.pdf
           ↑ relative to current directory

Special:
  .       = current directory
  ..      = parent directory
  ~       = home directory (/home/username)
  ~alice  = alice's home directory

Windows Examples

Absolute: C:\Users\Alice\Documents\report.pdf
           ↑ starts from drive letter

Relative: Documents\report.pdf
           ↑ relative to current directory

Special:
  .       = current directory
  ..      = parent directory
  %USERPROFILE% = home directory

Navigation Reference

graph TD
    ROOT["/"]
    ROOT --> HOME["/home"]
    ROOT --> ETC["/etc"]
    ROOT --> VAR["/var"]
    HOME --> ALICE["/home/alice"]
    HOME --> BOB["/home/bob"]
    ALICE --> DOCS["/home/alice/docs"]
    ALICE --> PROJ["/home/alice/projects"]

    CWD["πŸ“ CWD = /home/alice"]
    CWD -->|"cd .."| HOME
    CWD -->|"cd docs"| DOCS
    CWD -->|"cd /etc"| ETC
    CWD -->|"cd ~bob"| BOB
Loading

File Types in Linux

Linux represents everything as a file. The ls -l output shows the file type as the first character:

-rw-r--r--  1  alice  staff  1024  Apr 22 10:00  file.txt
drwxr-xr-x  2  alice  staff  4096  Apr 22 10:00  directory/
lrwxrwxrwx  1  alice  staff    10  Apr 22 10:00  link -> /etc/hosts
crw-rw-rw-  1  root   tty       5  Apr 22 10:00  /dev/tty
brw-rw----  1  root   disk    8,0  Apr 22 10:00  /dev/sda
prw-r--r--  1  alice  staff     0  Apr 22 10:00  mypipe
srwxrwxrwx  1  alice  staff     0  Apr 22 10:00  mysocket
First Char Type Description
- Regular file Text, binary, data
d Directory Container for files
l Symbolic link Pointer to another file
c Character device Serial, TTY (/dev/tty)
b Block device Disks (/dev/sda)
p Named pipe (FIFO) IPC between processes
s Socket Network/IPC socket

Inodes (Linux)

An inode (index node) stores file metadata β€” everything except the filename and actual data.

Directory Entry          Inode #1042              Data Blocks
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ "report.txt"  │──────▢│ inode: 1042    │──────▢│ "Hello World"β”‚
β”‚ inode: 1042   β”‚       β”‚ size: 12 bytes β”‚       β”‚ ...file data β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚ owner: alice   β”‚       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚ perms: 644     β”‚
                        β”‚ atime: ...     β”‚
                        β”‚ mtime: ...     β”‚
                        β”‚ ctime: ...     β”‚
                        β”‚ links: 1       β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • atime β€” Last access time
  • mtime β€” Last modification time (content)
  • ctime β€” Last change time (metadata or content)
stat report.txt       # Show inode info
ls -i                 # Show inode numbers
df -i                 # Show inode usage per filesystem

Hard Links vs Symbolic Links

Hard Link:
  file.txt ──┐
              β”œβ”€β”€β–Ά inode 1042 ──▢ Data Blocks
  hardlink  β”€β”€β”˜
  (same inode, both point to same data)

Symbolic Link:
  symlink.txt ──▢ "file.txt" (path string) ──▢ inode 1042 ──▢ Data
  (separate inode, stores path as data)
Feature Hard Link Symbolic Link
Inode Same as original Different
Cross-filesystem ❌ No βœ… Yes
Works on directories ❌ No (usually) βœ… Yes
Broken if original deleted ❌ No (data survives) βœ… Yes (dangling link)
Command ln file hardlink ln -s target link

Mounting File Systems

In Linux, storage devices are mounted to directories (mount points).

# View current mounts
mount
df -h                        # Disk usage with human-readable sizes
lsblk                        # Block device tree

# Mount a device
sudo mount /dev/sdb1 /mnt/usb

# Unmount
sudo umount /mnt/usb

# Auto-mount at boot (edit /etc/fstab)
# Device         Mount Point   FS Type  Options    Dump  Pass
/dev/sda1        /             ext4     defaults   0     1
/dev/sda2        /home         ext4     defaults   0     2
UUID=xxxx        /mnt/data     btrfs    defaults   0     0
tmpfs            /tmp          tmpfs    defaults   0     0

File Permissions Overview

Detailed coverage in 05_Permissions.md

-rwxr-xr--  alice  staff  file.sh
 ↑↑↑↑↑↑↑↑↑
 │└───└───└──── others: r-- (read only)
 β”‚   β”‚   └───── group: r-x (read + execute)
 β”‚   └────────── owner: rwx (full)
 └────────────── file type: - (regular)

Related Topics


← OS Fundamentals | Index | Linux CLI β†’