Linux File System

The Linux file system is a hierarchical structure that organizes files and directories in a logical manner. Understanding this structure is essential for effective system administration and navigation.

/ (Root directory) ├── bin/ (Essential user binaries) ├── boot/ (Boot loader files) ├── dev/ (Device files) ├── etc/ (System configuration) ├── home/ (User home directories) ├── lib/ (Essential shared libraries) ├── media/ (Removable media mount points) ├── mnt/ (Temporary mount points) ├── opt/ (Optional software packages) ├── proc/ (Process and kernel information) ├── root/ (Root user home directory) ├── run/ (Runtime data) ├── sbin/ (System binaries) ├── srv/ (Service data) ├── sys/ (System information) ├── tmp/ (Temporary files) ├── usr/ (User programs and data) └── var/ (Variable data)

Overview

The Linux file system follows the Filesystem Hierarchy Standard (FHS), which defines the structure and purpose of directories. Key concepts include:

  • Everything is a file - Devices, processes, and system information are represented as files
  • Single root hierarchy - All files and directories stem from the root directory (/)
  • Case sensitivity - File and directory names are case-sensitive
  • No drive letters - Storage devices are mounted as directories in the hierarchy
  • Hidden files - Files starting with a dot (.) are hidden by default

Standard Directory Structure

Directory Purpose Contents
/ Root directory Top-level directory containing all other directories
/bin Essential user binaries Basic commands like ls, cp, mv, cat, bash
/boot Boot loader files Kernel, initrd, bootloader configuration
/dev Device files Hardware devices, terminals, null, random
/etc System configuration Configuration files, startup scripts
/home User home directories Personal directories for regular users
/lib Essential shared libraries Libraries needed by /bin and /sbin programs
/media Removable media Mount points for USB drives, CDs, DVDs
/mnt Temporary mount points Manually mounted filesystems
/opt Optional software Third-party applications and packages
/proc Process information Virtual filesystem with process and kernel info
/root Root user home Home directory for the root user
/run Runtime data Runtime information since last boot
/sbin System binaries System administration commands
/srv Service data Data for services provided by the system
/sys System information Virtual filesystem for system and hardware info
/tmp Temporary files Temporary files, cleared on reboot
/usr User programs User applications, libraries, documentation
/var Variable data Logs, databases, mail, print queues

Important Subdirectories

/usr subdirectories

/usr/bin # User commands and applications /usr/sbin # Non-essential system binaries /usr/lib # Libraries for /usr/bin and /usr/sbin /usr/local # Locally installed software /usr/share # Architecture-independent data /usr/include # Header files for C programming /usr/src # Source code

The /usr directory contains the majority of user utilities and applications

/var subdirectories

/var/log # System and application logs /var/lib # Variable state information /var/cache # Application cache data /var/spool # Spool directories (mail, print) /var/tmp # Temporary files preserved between reboots /var/run # Runtime variable data (often symlink to /run)

The /var directory contains files that change during system operation

/etc subdirectories

/etc/passwd # User account information /etc/shadow # Encrypted passwords /etc/group # Group information /etc/fstab # Filesystem mount table /etc/hosts # Static hostname resolution /etc/crontab # System cron jobs /etc/init.d/ # System initialization scripts

The /etc directory contains system configuration files

File Types in Linux

Regular files (-)

-rw-r--r-- 1 user group 1024 Jan 21 10:30 document.txt

Standard files containing data, text, or executable code

Directories (d)

drwxr-xr-x 2 user group 4096 Jan 21 10:30 my_directory/

Containers that hold other files and directories

Symbolic links (l)

lrwxrwxrwx 1 user group 10 Jan 21 10:30 link -> target_file

Pointers to other files or directories

Character devices (c)

crw-rw-rw- 1 root tty 5, 0 Jan 21 10:30 /dev/tty

Devices that transfer data character by character (terminals, serial ports)

Block devices (b)

brw-rw---- 1 root disk 8, 0 Jan 21 10:30 /dev/sda

Devices that transfer data in blocks (hard drives, USB drives)

Named pipes (p)

prw-r--r-- 1 user group 0 Jan 21 10:30 my_pipe

Special files for inter-process communication

Sockets (s)

srwxrwxrwx 1 user group 0 Jan 21 10:30 socket_file

Special files for network or local inter-process communication

Common Filesystem Types

Filesystem Type Features Use Case
ext4 Journaling Stable, backward compatible, large file support Default for most Linux distributions
xfs Journaling High performance, large filesystems, parallel I/O High-performance servers, large storage
btrfs Copy-on-write Snapshots, compression, RAID, subvolumes Advanced features, system snapshots
zfs Copy-on-write Built-in RAID, compression, deduplication Enterprise storage, data integrity
ntfs Journaling Windows compatibility, large files Dual-boot systems, Windows compatibility
fat32 Simple Universal compatibility, simple structure USB drives, cross-platform compatibility
tmpfs Memory-based RAM-based, very fast, volatile Temporary files, /tmp, /run

File System Navigation

Basic navigation

pwd # Print working directory cd /path/to/dir # Change to specific directory cd ~ # Change to home directory cd .. # Go up one directory cd - # Go to previous directory

Essential commands for moving around the filesystem

Listing files and directories

ls # List files in current directory ls -l # Long format with details ls -la # Include hidden files ls -lh # Human-readable file sizes ls -R # Recursive listing

Various ways to list and examine directory contents

File information

file filename # Determine file type stat filename # Detailed file information du -sh directory/ # Directory size df -h # Filesystem usage

Commands to get detailed information about files and filesystems

Finding files

find /path -name "pattern" # Find files by name find /path -type f -size +100M # Find large files locate filename # Quick file search which command # Find command location whereis command # Find command and docs

Tools for locating files and programs in the filesystem

Virtual Filesystems

/proc filesystem

cat /proc/cpuinfo # CPU information cat /proc/meminfo # Memory information cat /proc/version # Kernel version ls /proc/[0-9]* # Running processes cat /proc/mounts # Mounted filesystems

Virtual filesystem providing process and kernel information

/sys filesystem

ls /sys/class/net/ # Network interfaces cat /sys/class/thermal/thermal_zone0/temp # CPU temperature ls /sys/block/ # Block devices ls /sys/bus/ # System buses

Virtual filesystem exposing kernel objects and hardware information

/dev filesystem

ls /dev/sd* # SCSI/SATA devices ls / dev/tty* # Terminal devices ls /dev/input/ # Input devices cat /dev/null # Null device cat /dev/random # Random data

Device files representing hardware and virtual devices

Mounting and Unmounting

View mounted filesystems

mount # Show all mounted filesystems df -h # Show mounted filesystems with usage cat /proc/mounts # Kernel view of mounts findmnt # Tree view of mounts

Commands to view currently mounted filesystems

Mount filesystems

sudo mount /dev/sdb1 /mnt/usb # Mount USB drive sudo mount -t ext4 /dev/sdc1 /mnt # Specify filesystem type sudo mount -o ro /dev/sdb1 /mnt # Mount read-only sudo mount --bind /source /target # Bind mount

Various ways to mount filesystems and devices

Unmount filesystems

sudo umount /mnt/usb # Unmount by mount point sudo umount /dev/sdb1 # Unmount by device sudo umount -l /mnt # Lazy unmount sudo umount -f /mnt # Force unmount

Commands to safely unmount filesystems

Automatic mounting (/etc/fstab)

# /etc/fstab format: # device mountpoint fstype options dump pass /dev/sda1 / ext4 defaults 0 1 /dev/sda2 /home ext4 defaults 0 2 /dev/sdb1 /mnt/data ext4 noauto 0 0 UUID=1234 /boot ext4 defaults 0 2

Configuration file for automatic filesystem mounting

File System Maintenance

Check filesystem integrity

sudo fsck /dev/sdb1 # Check filesystem sudo fsck -y /dev/sdb1 # Auto-repair errors sudo e2fsck /dev/sdb1 # Check ext2/3/4 filesystem sudo xfs_check /dev/sdb1 # Check XFS filesystem

Commands to check and repair filesystem errors

Resize filesystems

# Resize ext4 filesystem sudo resize2fs /dev/sdb1 # Resize XFS filesystem (must be mounted) sudo xfs_growfs /mount/point # Shrink ext4 (unmount first) sudo resize2fs /dev/sdb1 10G

Commands to resize filesystems after partition changes

Create filesystems

sudo mkfs.ext4 /dev/sdb1 # Create ext4 filesystem sudo mkfs.xfs /dev/sdb1 # Create XFS filesystem sudo mkfs.btrfs /dev/sdb1 # Create Btrfs filesystem sudo mkfs.fat -F32 /dev/sdb1 # Create FAT32 filesystem

Commands to create new filesystems on partitions

Best Practices

File System Organization
  • Follow the FHS (Filesystem Hierarchy Standard)
  • Keep user data in /home directories
  • Use /opt for third-party software
  • Store logs in /var/log
  • Use /tmp for temporary files
  • Keep system configuration in /etc
Performance and Maintenance
  • Regular filesystem checks with fsck
  • Monitor disk usage with df and du
  • Use appropriate filesystem types for different use cases
  • Implement proper backup strategies
  • Consider using LVM for flexible storage management
  • Clean up temporary files regularly
Security Considerations
  • Set appropriate file permissions
  • Use separate partitions for different purposes
  • Mount filesystems with security options (noexec, nosuid)
  • Regularly audit file permissions and ownership
  • Encrypt sensitive data partitions
  • Monitor filesystem access and modifications

Common File System Tasks

Find large files and directories

# Find largest directories du -h /home | sort -hr | head -10 # Find files larger than 100MB find /home -type f -size +100M -exec ls -lh {} \; # Find largest files in current directory ls -lhS

Identify space usage and large files

Clean up disk space

# Clean package cache sudo apt clean # Debian/Ubuntu sudo yum clean all # RHEL/CentOS # Clean temporary files sudo rm -rf /tmp/* sudo rm -rf /var/tmp/* # Clean old log files sudo journalctl --vacuum-time=30d

Free up disk space by removing unnecessary files

Monitor filesystem usage

# Real-time disk usage watch -n 1 'df -h' # Monitor directory size changes watch -n 5 'du -sh /var/log' # Check inode usage df -i

Monitor filesystem usage and changes over time

Troubleshooting

Filesystem full errors

# Check disk usage df -h # Find what's using space du -h / | sort -hr | head -20 # Check for deleted files still held open lsof +L1

Diagnose and resolve "No space left on device" errors

Permission denied errors

# Check file permissions ls -la filename # Check directory permissions in path ls -ld /path/to/directory # Check mount options mount | grep partition

Troubleshoot file access permission issues

Filesystem corruption

# Check filesystem (unmount first) sudo umount /dev/sdb1 sudo fsck -y /dev/sdb1 # Force check on next boot sudo touch /forcefsck # Check filesystem in read-only mode sudo fsck -n /dev/sdb1

Handle filesystem corruption and errors

See also