/void !

Linux File System

2 min read

OverviewπŸ”—

The Linux filesystem follows the Filesystem Hierarchy Standard (FHS), which defines the directory structure and contents in Unix-like operating systems.

Key Principles:

  • Everything is a file (including devices)
  • Single root directory (/)
  • Case-sensitive filenames
  • Hidden files start with dot (.)
  • No file extensions required

Filesystem Hierarchy Standard (FHS)πŸ”—

Root Directory (/)πŸ”—

The top-level directory from which all other directories branch.

/
β”œβ”€β”€ bin     -> usr/bin    # Essential user binaries
β”œβ”€β”€ boot                  # Boot loader files
β”œβ”€β”€ dev                   # Device files
β”œβ”€β”€ etc                   # System configuration files
β”œβ”€β”€ home                  # User home directories
β”œβ”€β”€ lib     -> usr/lib    # Shared libraries
β”œβ”€β”€ media                 # Mount points for removable media
β”œβ”€β”€ mnt                   # Temporary mount points
β”œβ”€β”€ opt                   # Optional/third-party software
β”œβ”€β”€ proc                  # Process and kernel information
β”œβ”€β”€ root                  # Root user home directory
β”œβ”€β”€ run                   # Runtime data
β”œβ”€β”€ sbin    -> usr/sbin   # System binaries
β”œβ”€β”€ srv                   # Service data
β”œβ”€β”€ sys                   # Kernel and system information
β”œβ”€β”€ tmp                   # Temporary files
β”œβ”€β”€ usr                   # User programs and data
└── var                   # Variable data files

/bin - Essential User BinariesπŸ”—

# Symlinked to /usr/bin on modern systems
ls /bin
# Common binaries: bash, cat, cp, ls, mv, rm, echo, grep, etc.

/boot - Boot Loader FilesπŸ”—

/boot/
β”œβ”€β”€ vmlinuz-*          # Linux kernel
β”œβ”€β”€ initrd.img-*       # Initial RAM disk
β”œβ”€β”€ grub/              # GRUB bootloader
└── config-*           # Kernel configuration

/dev - Device FilesπŸ”—

/dev/
β”œβ”€β”€ sda, sdb           # SATA/SCSI disks
β”œβ”€β”€ sda1, sda2         # Disk partitions
β”œβ”€β”€ nvme0n1            # NVMe drives
β”œβ”€β”€ tty*               # Terminals
β”œβ”€β”€ null               # Null device (discards all data)
β”œβ”€β”€ zero               # Zero device (provides zeros)
β”œβ”€β”€ random, urandom    # Random number generators
└── loop*              # Loop devices

# Examples
ls -l /dev/sda         # View disk device
ls -l /dev/tty1        # View terminal device

/etc - System ConfigurationπŸ”—

/etc/
β”œβ”€β”€ passwd             # User account information
β”œβ”€β”€ shadow             # Encrypted passwords
β”œβ”€β”€ group              # Group information
β”œβ”€β”€ fstab              # Filesystem mount table
β”œβ”€β”€ hosts              # Static hostname lookup
β”œβ”€β”€ hostname           # System hostname
β”œβ”€β”€ resolv.conf        # DNS resolver configuration
β”œβ”€β”€ network/           # Network configuration
β”œβ”€β”€ ssh/               # SSH configuration
β”œβ”€β”€ systemd/           # Systemd configuration
β”œβ”€β”€ apt/               # APT configuration (Debian/Ubuntu)
β”œβ”€β”€ yum/               # YUM configuration (RHEL/CentOS)
└── cron.d/            # Cron job configurations

# Examples
cat /etc/passwd        # View user accounts
cat /etc/hosts         # View hostname mappings
cat /etc/fstab         # View filesystem mounts

/home - User Home DirectoriesπŸ”—

/home/
β”œβ”€β”€ user1/
β”‚   β”œβ”€β”€ .bashrc        # Bash configuration
β”‚   β”œβ”€β”€ .profile       # Shell profile
β”‚   β”œβ”€β”€ .ssh/          # SSH keys
β”‚   β”œβ”€β”€ Documents/
β”‚   β”œβ”€β”€ Downloads/
β”‚   └── .local/        # User-specific data
└── user2/

# User home directory shortcut
echo $HOME             # /home/username
cd ~                   # Go to home directory
cd ~/Documents         # Go to Documents

/lib - Shared LibrariesπŸ”—

# Symlinked to /usr/lib on modern systems
/lib/
β”œβ”€β”€ modules/           # Kernel modules
β”œβ”€β”€ firmware/          # Hardware firmware
└── *.so               # Shared library files

# Examples
ls /lib/modules/$(uname -r)/  # Current kernel modules

/media - Removable MediaπŸ”—

/media/
β”œβ”€β”€ username/
β”‚   β”œβ”€β”€ USB_DRIVE/
β”‚   └── CDROM/

# Auto-mounted removable devices

/mnt - Temporary MountsπŸ”—

# Manual mount point for temporary filesystems
sudo mount /dev/sdb1 /mnt
sudo umount /mnt

/opt - Optional SoftwareπŸ”—

/opt/
β”œβ”€β”€ google/            # Google Chrome
β”œβ”€β”€ teamviewer/
└── custom-app/

# Third-party applications that don't follow FHS

/proc - Process InformationπŸ”—

/proc/
β”œβ”€β”€ 1234/              # Process ID directory
β”œβ”€β”€ cpuinfo            # CPU information
β”œβ”€β”€ meminfo            # Memory information
β”œβ”€β”€ version            # Kernel version
└── uptime             # System uptime

# Examples
cat /proc/cpuinfo      # View CPU info
cat /proc/meminfo      # View memory info
cat /proc/version      # View kernel version
ls /proc/1/            # View init process info

/root - Root Home DirectoryπŸ”—

# Home directory for root user
/root/
β”œβ”€β”€ .bashrc
β”œβ”€β”€ .ssh/
└── scripts/

# Access
sudo -i                # Switch to root

/run - Runtime DataπŸ”—

/run/
β”œβ”€β”€ systemd/           # Systemd runtime data
β”œβ”€β”€ user/              # User runtime directories
└── lock/              # Lock files

# Cleared on reboot

/srv - Service DataπŸ”—

/srv/
β”œβ”€β”€ www/               # Web server data
β”œβ”€β”€ ftp/               # FTP server data
└── git/               # Git repositories

# Data for services provided by the system

/sys - Kernel/Hardware InfoπŸ”—

/sys/
β”œβ”€β”€ block/             # Block devices
β”œβ”€β”€ bus/               # Bus types
β”œβ”€β”€ class/             # Device classes
β”œβ”€β”€ devices/           # Physical devices
└── module/            # Kernel modules

# Examples
cat /sys/class/power_supply/BAT0/capacity  # Battery level
cat /sys/class/thermal/thermal_zone0/temp   # CPU temperature

/tmp - Temporary FilesπŸ”—

/tmp/
# Cleared periodically (usually on reboot)
# World-writable with sticky bit

# Create temp file
mktemp /tmp/myfile.XXXXXX

# Permissions
ls -ld /tmp
# drwxrwxrwt (sticky bit set)

/usr - User ProgramsπŸ”—

/usr/
β”œβ”€β”€ bin/               # User commands
β”œβ”€β”€ sbin/              # System administration commands
β”œβ”€β”€ lib/               # Libraries
β”œβ”€β”€ share/             # Architecture-independent data
β”‚   β”œβ”€β”€ man/           # Manual pages
β”‚   β”œβ”€β”€ doc/           # Documentation
β”‚   └── applications/  # Desktop entries
β”œβ”€β”€ local/             # Locally installed software
β”‚   β”œβ”€β”€ bin/
β”‚   β”œβ”€β”€ lib/
β”‚   └── share/
β”œβ”€β”€ include/           # C header files
└── src/               # Source code

# Examples
ls /usr/bin            # User binaries
man -k keyword         # Search man pages

/var - Variable DataπŸ”—

/var/
β”œβ”€β”€ log/               # Log files
β”‚   β”œβ”€β”€ syslog         # System log
β”‚   β”œβ”€β”€ auth.log       # Authentication log
β”‚   β”œβ”€β”€ kern.log       # Kernel log
β”‚   └── apache2/       # Apache logs
β”œβ”€β”€ cache/             # Application cache
β”œβ”€β”€ tmp/               # Temporary files (preserved across reboots)
β”œβ”€β”€ spool/             # Spool files
β”‚   β”œβ”€β”€ cron/          # Cron jobs
β”‚   └── mail/          # Mail queue
β”œβ”€β”€ lib/               # Variable state information
β”‚   β”œβ”€β”€ dpkg/          # Package manager database
β”‚   └── mysql/         # MySQL databases
β”œβ”€β”€ www/               # Web server files
└── run/               # Runtime data (symlink to /run)

# Examples
tail -f /var/log/syslog          # Monitor system log
ls /var/cache/apt/archives/      # APT package cache

File PermissionsπŸ”—

Permission TypesπŸ”—

# Format: type|owner|group|others
# drwxr-xr-x
# d = directory
# rwx = read, write, execute for owner
# r-x = read, execute for group
# r-x = read, execute for others

# Permission values
r (read)    = 4
w (write)   = 2
x (execute) = 1
- (none)    = 0

Viewing PermissionsπŸ”—

# Long listing
ls -l file.txt
# -rw-r--r-- 1 user group 1234 Jan 15 10:00 file.txt

# Just permissions
stat -c '%A' file.txt
# -rw-r--r--

# Numeric permissions
stat -c '%a' file.txt
# 644

chmod - Change ModeπŸ”—

# Symbolic mode
chmod u+x file.sh          # Add execute for user
chmod g-w file.txt         # Remove write for group
chmod o+r file.txt         # Add read for others
chmod a+x file.sh          # Add execute for all
chmod u=rw,g=r,o=r file    # Set exact permissions

# Numeric mode
chmod 755 file.sh          # rwxr-xr-x
chmod 644 file.txt         # rw-r--r--
chmod 600 private.key      # rw-------
chmod 777 script.sh        # rwxrwxrwx (avoid!)

# Recursive
chmod -R 755 directory/    # Apply to directory and contents

# Common permissions
chmod 755 script.sh        # Executable script
chmod 644 file.txt         # Regular file
chmod 600 ~/.ssh/id_rsa    # Private key
chmod 700 ~/.ssh           # SSH directory
chmod 1777 /tmp            # Sticky bit directory

chown - Change OwnershipπŸ”—

# Change owner
sudo chown user file.txt

# Change owner and group
sudo chown user:group file.txt

# Change only group
sudo chown :group file.txt
# or
sudo chgrp group file.txt

# Recursive
sudo chown -R user:group directory/

# Examples
sudo chown www-data:www-data /var/www/html/
sudo chown -R $USER:$USER ~/project/

Special PermissionsπŸ”—

# Setuid (4) - Execute as file owner
chmod u+s file             # or chmod 4755 file
# Example: /usr/bin/passwd

# Setgid (2) - Execute as group / inherit directory group
chmod g+s directory        # or chmod 2755 directory
# Files created inherit directory group

# Sticky bit (1) - Only owner can delete
chmod +t directory         # or chmod 1777 directory
# Example: /tmp

umask - Default PermissionsπŸ”—

# View current umask
umask
# 0022

# Set umask
umask 0027               # Files: 640, Directories: 750

# Calculate default permissions
# Files: 666 - umask
# Directories: 777 - umask

# Examples
umask 0022               # Files: 644, Dirs: 755 (default user)
umask 0077               # Files: 600, Dirs: 700 (private)
umask 0002               # Files: 664, Dirs: 775 (group writable)

# Set permanently in ~/.bashrc
echo "umask 0027" >> ~/.bashrc

Access Control Lists (ACL)πŸ”—

View ACLπŸ”—

# Check if ACL is supported
mount | grep acl

# View ACL
getfacl file.txt

Set ACLπŸ”—

# Give user read/write
setfacl -m u:username:rw file.txt

# Give group read
setfacl -m g:groupname:r file.txt

# Set default ACL for directory
setfacl -d -m u:username:rwx directory/

# Remove ACL
setfacl -x u:username file.txt

# Remove all ACL
setfacl -b file.txt

# Recursive
setfacl -R -m u:username:rwx directory/

# Copy ACL
getfacl file1 | setfacl --set-file=- file2

Filesystem TypesπŸ”—

Common FilesystemsπŸ”—

# ext4 - Fourth Extended Filesystem (default on most Linux)
# xfs - XFS filesystem (default on RHEL/CentOS 7+)
# btrfs - B-tree filesystem (CoW, snapshots)
# f2fs - Flash-Friendly Filesystem (for SSDs)
# zfs - ZFS filesystem (advanced features)
# vfat/exfat - FAT filesystems (Windows compatibility)
# ntfs - NTFS filesystem (Windows)

# Check filesystem type
df -T
lsblk -f
blkid /dev/sda1

Mount/UnmountπŸ”—

# Mount filesystem
sudo mount /dev/sdb1 /mnt
sudo mount -t ext4 /dev/sdb1 /mnt

# Mount with options
sudo mount -o ro /dev/sdb1 /mnt           # Read-only
sudo mount -o rw,noexec /dev/sdb1 /mnt    # No execution

# Unmount
sudo umount /mnt
sudo umount /dev/sdb1

# Force unmount
sudo umount -f /mnt
sudo umount -l /mnt                        # Lazy unmount

# Check what's using the mount
lsof +D /mnt
fuser -m /mnt

/etc/fstab - Permanent MountsπŸ”—

# Format: device  mountpoint  type  options  dump  pass
UUID=xxx  /data  ext4  defaults  0  2
/dev/sdb1  /backup  ext4  defaults,noatime  0  2
//server/share  /mnt/smb  cifs  credentials=/root/.smbcred  0  0
tmpfs  /tmp  tmpfs  defaults,noatime,mode=1777  0  0

# Mount all from fstab
sudo mount -a

# Reload systemd after fstab changes
sudo systemctl daemon-reload

Disk ManagementπŸ”—

Disk InformationπŸ”—

# List block devices
lsblk
lsblk -f                   # With filesystem info

# Disk usage
df -h                      # Human-readable
df -i                      # Inode usage
df -T                      # Filesystem type

# Directory size
du -sh directory/          # Summary
du -h --max-depth=1 .      # First level subdirs
du -hac directory/         # All with total

# Largest directories
du -hx / | sort -rh | head -20

# Find large files
find / -type f -size +100M 2>/dev/null
find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null

Partition ManagementπŸ”—

# List partitions
sudo fdisk -l
sudo parted -l
lsblk

# Create partition (fdisk)
sudo fdisk /dev/sdb
# n (new), p (primary), w (write)

# Create partition (parted)
sudo parted /dev/sdb
# mklabel gpt
# mkpart primary ext4 0% 100%

# Format partition
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb1
sudo mkfs.vfat -F 32 /dev/sdb1

Filesystem Check and RepairπŸ”—

# Check filesystem (unmounted!)
sudo fsck /dev/sda1
sudo e2fsck -f /dev/sda1   # ext2/3/4
sudo xfs_repair /dev/sda1   # XFS

# Force check on next reboot
sudo touch /forcefsck

# Disk check with badblocks
sudo badblocks -v /dev/sda
# Create symlink
ln -s /path/to/original /path/to/link

# Examples
ln -s /usr/bin/python3 ~/bin/python
ln -s /var/www/html ~/www
ln -s ../../../file.txt relative-link.txt

# View symlink
ls -l link
# lrwxrwxrwx 1 user group 15 Jan 15 10:00 link -> /path/to/original

# Read symlink target
readlink link
readlink -f link           # Follow all symlinks

# Remove symlink
rm link                    # Don't use trailing slash!
unlink link
# Create hard link
ln /path/to/original /path/to/link

# View inode
ls -li file
# 1234567 -rw-r--r-- 2 user group 100 Jan 15 10:00 file

# Hard link count in ls output
ls -l file
# -rw-r--r-- 2 user group ...
#            ^ link count

# Find all hard links to file
find / -inum 1234567 2>/dev/null

DifferencesπŸ”—

# Symbolic Link:
# - Can link to directories
# - Can cross filesystems
# - Breaks if original is deleted
# - Shows -> in ls output

# Hard Link:
# - Cannot link to directories
# - Must be on same filesystem
# - Remains valid if original is deleted
# - All links are equal (no "original")

File AttributesπŸ”—

Extended AttributesπŸ”—

# View attributes
lsattr file.txt

# Set attributes
sudo chattr +i file.txt    # Immutable (can't modify/delete)
sudo chattr +a file.txt    # Append only
sudo chattr -i file.txt    # Remove immutable

# Common attributes
# i = immutable
# a = append only
# A = no atime update
# d = no dump
# s = secure deletion

File CapabilitiesπŸ”—

# View capabilities
getcap /usr/bin/ping

# Set capability
sudo setcap cap_net_raw+ep /usr/bin/ping

# Remove capability
sudo setcap -r /usr/bin/ping

# Common capabilities
# cap_net_raw = raw network access
# cap_net_admin = network admin
# cap_sys_admin = system admin

Finding FilesπŸ”—

find CommandπŸ”—

# By name
find /path -name "*.txt"
find /path -iname "*.TXT"      # Case-insensitive

# By type
find /path -type f             # Files
find /path -type d             # Directories
find /path -type l             # Symlinks

# By size
find /path -size +100M         # Larger than 100MB
find /path -size -1M           # Smaller than 1MB

# By modification time
find /path -mtime -7           # Modified in last 7 days
find /path -mtime +30          # Modified more than 30 days ago
find /path -mmin -60           # Modified in last 60 minutes

# By permissions
find /path -perm 644
find /path -perm -u+w          # User writable

# By owner
find /path -user username
find /path -group groupname

# Execute command
find /path -name "*.log" -exec rm {} \;
find /path -name "*.txt" -delete

# Combine conditions
find /path -name "*.log" -size +10M -mtime +30

locate CommandπŸ”—

# Update database
sudo updatedb

# Search
locate filename
locate -i filename         # Case-insensitive
locate -n 10 filename      # Limit results

# Count matches
locate -c filename

Best PracticesπŸ”—

PermissionsπŸ”—

  • Use least privilege principle
  • Avoid 777 permissions
  • Use ACL for fine-grained control
  • Protect sensitive files (600 or 400)
  • Use sticky bit on shared directories

OrganizationπŸ”—

  • Follow FHS standards
  • Keep user data in /home
  • Use /opt for third-party software
  • Store logs in /var/log
  • Use /tmp for temporary files

SecurityπŸ”—

  • Regularly check permissions on sensitive files
  • Monitor /etc for unauthorized changes
  • Use immutable attribute on critical files
  • Audit setuid/setgid binaries
  • Keep /tmp with noexec option

MaintenanceπŸ”—

  • Monitor disk usage regularly
  • Clean old logs from /var/log
  • Remove unused packages
  • Check for orphaned files
  • Backup /etc configuration

ResourcesπŸ”—

  • FHS 3.0: https://refspecs.linuxfoundation.org/FHS_3.0/
  • Linux Documentation Project: https://tldp.org/
  • Arch Wiki: https://wiki.archlinux.org/
  • man pages: man hier, man chmod, man mount