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.
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
The /usr directory contains the majority of user utilities and applications
/var subdirectories
The /var directory contains files that change during system operation
/etc subdirectories
The /etc directory contains system configuration files
File Types in Linux
Regular files (-)
Standard files containing data, text, or executable code
Directories (d)
Containers that hold other files and directories
Symbolic links (l)
Pointers to other files or directories
Character devices (c)
Devices that transfer data character by character (terminals, serial ports)
Block devices (b)
Devices that transfer data in blocks (hard drives, USB drives)
Named pipes (p)
Special files for inter-process communication
Sockets (s)
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
Essential commands for moving around the filesystem
Listing files and directories
Various ways to list and examine directory contents
File information
Commands to get detailed information about files and filesystems
Finding files
Tools for locating files and programs in the filesystem
Virtual Filesystems
/proc filesystem
Virtual filesystem providing process and kernel information
/sys filesystem
Virtual filesystem exposing kernel objects and hardware information
/dev filesystem
Device files representing hardware and virtual devices
Mounting and Unmounting
View mounted filesystems
Commands to view currently mounted filesystems
Mount filesystems
Various ways to mount filesystems and devices
Unmount filesystems
Commands to safely unmount filesystems
Automatic mounting (/etc/fstab)
Configuration file for automatic filesystem mounting
File System Maintenance
Check filesystem integrity
Commands to check and repair filesystem errors
Resize filesystems
Commands to resize filesystems after partition changes
Create filesystems
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
Identify space usage and large files
Clean up disk space
Free up disk space by removing unnecessary files
Monitor filesystem usage
Monitor filesystem usage and changes over time
Troubleshooting
Filesystem full errors
Diagnose and resolve "No space left on device" errors
Permission denied errors
Troubleshoot file access permission issues
Filesystem corruption
Handle filesystem corruption and errors