Linux File System Hierarchy: Complete Guide
Table of Contents
1. [Introduction](#introduction) 2. [File System Hierarchy Standard (FHS)](#file-system-hierarchy-standard-fhs) 3. [Root Directory Structure](#root-directory-structure) 4. [Detailed Directory Descriptions](#detailed-directory-descriptions) 5. [File Types in Linux](#file-types-in-linux) 6. [Navigation Commands](#navigation-commands) 7. [File System Operations](#file-system-operations) 8. [Permissions and Ownership](#permissions-and-ownership) 9. [Mount Points and File Systems](#mount-points-and-file-systems) 10. [Best Practices](#best-practices)Introduction
The Linux file system hierarchy is a standardized directory structure that organizes files and directories in a logical, hierarchical manner. Unlike Windows systems that use drive letters (C:, D:, etc.), Linux uses a single root directory (/) from which all other directories branch out like an inverted tree structure.
Understanding the file system hierarchy is crucial for: - System administration - Software development - Troubleshooting - Security management - Efficient system navigation
File System Hierarchy Standard (FHS)
The Filesystem Hierarchy Standard (FHS) defines the directory structure and directory contents in Linux distributions. It ensures consistency across different Linux distributions and provides guidelines for where specific types of files should be stored.
FHS Compliance Levels
| Level | Description | Examples |
|-------|-------------|----------|
| Required | Must be present | /bin, /etc, /lib, /root, /tmp |
| Optional | May be present | /home, /mnt, /opt, /srv |
| Deprecated | Should be avoided | /usr/tmp (use /tmp instead) |
Root Directory Structure
The root directory (/) is the top-level directory in the Linux file system hierarchy. All other directories are subdirectories of the root directory.
`
/
├── bin/ # Essential user binaries
├── boot/ # Boot loader files
├── dev/ # Device files
├── etc/ # System configuration files
├── home/ # User home directories
├── lib/ # Essential shared libraries
├── lib64/ # 64-bit 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 files
`
Detailed Directory Descriptions
/bin (Essential User Binaries)
Contains essential command binaries that need to be available in single-user mode and for all users.
Common files:
- ls - List directory contents
- cp - Copy files
- mv - Move/rename files
- rm - Remove files
- cat - Display file contents
- bash - Bash shell
- grep - Text search utility
Command to explore:
`bash
ls -la /bin
file /bin/ls
which ls
`
Notes:
- These binaries are essential for system recovery
- Available even when /usr is not mounted
- Symlinked to /usr/bin in many modern distributions
/boot (Boot Loader Files)
Contains files needed for the boot process, including the kernel and initial RAM disk.
Typical contents:
| File/Directory | Description |
|----------------|-------------|
| vmlinuz-* | Compressed Linux kernel |
| initrd.img-* | Initial RAM disk |
| grub/ | GRUB bootloader configuration |
| config-* | Kernel configuration |
| System.map-* | Kernel symbol table |
Commands to explore:
`bash
ls -la /boot
du -h /boot
cat /boot/grub/grub.cfg
`
Notes: - Often mounted as separate partition - Critical for system boot process - Requires careful handling during updates
/dev (Device Files)
Contains device files that represent hardware devices and virtual devices.
Device file types:
| Type | Description | Example |
|------|-------------|---------|
| Block devices | Storage devices | /dev/sda1, /dev/nvme0n1 |
| Character devices | Serial devices | /dev/tty1, /dev/random |
| Symbolic links | Device aliases | /dev/cdrom → /dev/sr0 |
Common device files:
`bash
Storage devices
/dev/sda # First SCSI/SATA drive /dev/sdb1 # First partition of second drive /dev/nvme0n1 # NVMe SSDTerminal devices
/dev/tty1 # First virtual terminal /dev/pts/0 # Pseudo-terminalSpecial devices
/dev/null # Null device (discards all data) /dev/zero # Produces null bytes /dev/random # Random number generator /dev/urandom # Pseudo-random number generator`Exploration commands:
`bash
ls -la /dev
lsblk
cat /proc/devices
mknod /tmp/mydevice c 1 3 # Create character device
`
/etc (Configuration Files)
Contains system-wide configuration files and shell scripts used during boot.
Important subdirectories and files:
| Path | Purpose |
|------|---------|
| /etc/passwd | User account information |
| /etc/shadow | Encrypted passwords |
| /etc/group | Group information |
| /etc/fstab | File system table |
| /etc/hosts | Static hostname resolution |
| /etc/resolv.conf | DNS resolver configuration |
| /etc/crontab | System-wide cron jobs |
| /etc/init.d/ | System service scripts |
| /etc/systemd/ | Systemd configuration |
| /etc/ssh/ | SSH configuration |
Example configurations:
`bash
View system information
cat /etc/os-release cat /etc/hostname cat /etc/timezoneNetwork configuration
cat /etc/hosts cat /etc/resolv.conf cat /etc/network/interfaces # Debian/UbuntuUser management
cat /etc/passwd | head -5 cat /etc/group | head -5`/home (User Home Directories)
Contains personal directories for regular users.
Structure:
`
/home/
├── user1/
│ ├── .bashrc
│ ├── .profile
│ ├── Documents/
│ ├── Downloads/
│ └── Desktop/
└── user2/
├── .bashrc
└── projects/
`
Commands:
`bash
Navigate to home directory
cd ~ cd $HOME cd /home/$USERList user home directories
ls -la /homeCheck disk usage
du -sh /home/*`/lib and /lib64 (Essential Shared Libraries)
Contains shared library files needed by binaries in /bin and /sbin.
Library types:
| Extension | Type | Purpose |
|-----------|------|---------|
| .so | Shared object | Dynamic libraries |
| .a | Archive | Static libraries |
| .ko | Kernel object | Kernel modules |
Commands:
`bash
List libraries
ls /lib ls /lib64Check library dependencies
ldd /bin/ls ldconfig -p | grep libcFind library files
find /lib -name ".so" | head -10`/media and /mnt (Mount Points)
/media: Automatic mount points for removable media /mnt: Manual mount points for temporary file systems
Usage examples:
`bash
Media mounts (automatic)
ls /media ls /media/$USERManual mounts
sudo mkdir /mnt/external sudo mount /dev/sdb1 /mnt/external sudo umount /mnt/externalCheck mounted file systems
mount | grep /media df -h`/opt (Optional Software)
Contains add-on software packages that are not part of the default installation.
Typical structure:
`
/opt/
├── google/
│ └── chrome/
├── teamviewer/
└── custom-software/
├── bin/
├── lib/
└── etc/
`
/proc (Process Information)
Virtual file system that provides information about running processes and kernel parameters.
Important files and directories:
| Path | Information |
|------|-------------|
| /proc/cpuinfo | CPU information |
| /proc/meminfo | Memory information |
| /proc/version | Kernel version |
| /proc/uptime | System uptime |
| /proc/loadavg | System load average |
| /proc/[PID]/ | Process-specific information |
Examples:
`bash
System information
cat /proc/cpuinfo cat /proc/meminfo cat /proc/version cat /proc/uptimeProcess information
ls /proc/$ # Current shell process cat /proc/$/cmdline cat /proc/$/environ`/root (Root User Home)
Home directory for the root user (system administrator).
Characteristics:
- Located at /root, not /home/root
- Contains root's personal files and configurations
- Accessible only by root user
`bash
Switch to root (if permitted)
sudo su - cd /root ls -laView root's environment
sudo ls -la /root`/run (Runtime Data)
Contains runtime data for processes started since last boot.
Contents: - Process ID files - Socket files - Temporary runtime data
`bash
ls /run
ls /run/user/$UID # User runtime directory
`
/sbin (System Binaries)
Contains essential system administration binaries.
Common utilities:
- fdisk - Disk partitioning
- fsck - File system check
- iptables - Firewall configuration
- mount/umount - Mount file systems
- ifconfig - Network interface configuration
`bash
ls /sbin
which fdisk
/sbin/fdisk -l # List disk partitions
`
/srv (Service Data)
Contains data for services provided by the system.
Examples:
`
/srv/
├── www/ # Web server data
├── ftp/ # FTP server data
└── git/ # Git repositories
`
/sys (System Information)
Virtual file system providing information about devices, drivers, and kernel features.
`bash
Hardware information
ls /sys/class ls /sys/devices cat /sys/class/net/*/address # Network MAC addresses`/tmp (Temporary Files)
Directory for temporary files that may be deleted between reboots.
Characteristics: - World-writable with sticky bit - Often mounted as tmpfs (RAM-based) - Files may be automatically cleaned
`bash
ls -ld /tmp
df /tmp
Create temporary file
mktemp /tmp/myfile.XXXXXX`/usr (User Programs)
Contains user programs and supporting files.
Subdirectories:
| Directory | Purpose |
|-----------|---------|
| /usr/bin | User binaries |
| /usr/sbin | System administration binaries |
| /usr/lib | Libraries for /usr/bin and /usr/sbin |
| /usr/share | Shared data (documentation, icons) |
| /usr/local | Locally installed software |
| /usr/src | Source code |
| /usr/include | Header files |
/var (Variable Data)
Contains files that change frequently during system operation.
Important subdirectories:
| Directory | Purpose |
|-----------|---------|
| /var/log | Log files |
| /var/cache | Application cache |
| /var/spool | Spool directories (mail, print) |
| /var/tmp | Temporary files preserved between reboots |
| /var/lib | Variable state information |
| /var/run | Runtime variable data (often symlinked to /run) |
File Types in Linux
Linux recognizes several file types, each indicated by the first character in the long listing format.
| Symbol | Type | Description | Example |
|--------|------|-------------|---------|
| - | Regular file | Normal files | ls -l file.txt |
| d | Directory | Folders | drwxr-xr-x 2 user group |
| l | Symbolic link | Links to other files | lrwxrwxrwx 1 root root |
| c | Character device | Serial devices | crw-rw-rw- 1 root tty |
| b | Block device | Block devices | brw-rw---- 1 root disk |
| p | Named pipe | FIFO pipes | prw-r--r-- 1 user group |
| s | Socket | Unix sockets | srwxrwxrwx 1 user group |
Commands to identify file types:
`bash
file /bin/ls
file /dev/sda1
file /etc/passwd
ls -la | head -10
`
Navigation Commands
Basic Navigation
| Command | Purpose | Example |
|---------|---------|---------|
| pwd | Print working directory | pwd |
| cd | Change directory | cd /etc |
| ls | List directory contents | ls -la |
| tree | Display directory tree | tree /etc |
Advanced Navigation
`bash
Change to previous directory
cd -Go to parent directory
cd ..Go to home directory
cd ~ cd $HOMEGo to root directory
cd /Navigate using absolute paths
cd /usr/local/binNavigate using relative paths
cd ../share/doc`Directory Listing Options
`bash
Long format with details
ls -lInclude hidden files
ls -laHuman-readable sizes
ls -lhSort by modification time
ls -ltRecursive listing
ls -RList directories only
ls -d */List with inode numbers
ls -li`File System Operations
Creating Files and Directories
`bash
Create empty file
touch filename.txtCreate multiple files
touch file1.txt file2.txt file3.txtCreate directory
mkdir dirnameCreate nested directories
mkdir -p path/to/nested/directoryCreate directory with specific permissions
mkdir -m 755 dirname`Copying and Moving
`bash
Copy file
cp source.txt destination.txtCopy directory recursively
cp -r sourcedir/ destdir/Copy preserving attributes
cp -p file.txt backup.txtMove/rename file
mv oldname.txt newname.txtMove to different directory
mv file.txt /path/to/destination/`Removing Files and Directories
`bash
Remove file
rm filename.txtRemove multiple files
rm file1.txt file2.txtRemove directory and contents
rm -r dirname/Force removal without prompts
rm -f filename.txtInteractive removal
rm -i filename.txtRemove empty directory
rmdir dirname`Finding Files
`bash
Find by name
find /path -name "filename"Find by type
find /path -type f # files find /path -type d # directoriesFind by size
find /path -size +100M # larger than 100MB find /path -size -1k # smaller than 1KBFind by permissions
find /path -perm 755Find and execute command
find /path -name "*.log" -exec rm {} \;Locate command (faster, uses database)
locate filename updatedb # Update locate database`Permissions and Ownership
Understanding Permissions
Linux uses a three-tiered permission system: - User (u): File owner - Group (g): Group members - Other (o): Everyone else
Permission types: - Read (r): View file contents or list directory - Write (w): Modify file or directory contents - Execute (x): Run file or access directory
Permission Representation
| Octal | Binary | Symbolic | Permissions | |-------|--------|----------|-------------| | 0 | 000 | --- | No permissions | | 1 | 001 | --x | Execute only | | 2 | 010 | -w- | Write only | | 3 | 011 | -wx | Write and execute | | 4 | 100 | r-- | Read only | | 5 | 101 | r-x | Read and execute | | 6 | 110 | rw- | Read and write | | 7 | 111 | rwx | All permissions |
Changing Permissions
`bash
Using octal notation
chmod 755 filename # rwxr-xr-x chmod 644 filename # rw-r--r-- chmod 600 filename # rw-------Using symbolic notation
chmod u+x filename # Add execute for user chmod g-w filename # Remove write for group chmod o=r filename # Set read-only for others chmod a+r filename # Add read for allRecursive permission change
chmod -R 755 directory/`Changing Ownership
`bash
Change owner
chown newowner filenameChange owner and group
chown newowner:newgroup filenameChange group only
chgrp newgroup filenameRecursive ownership change
chown -R user:group directory/`Special Permissions
| Permission | Octal | Effect | |------------|-------|--------| | Sticky bit | 1000 | Only owner can delete files in directory | | SGID | 2000 | Execute with group privileges | | SUID | 4000 | Execute with owner privileges |
`bash
Set sticky bit (commonly on /tmp)
chmod +t directory/ chmod 1755 directory/Set SUID bit
chmod +s executable chmod 4755 executableSet SGID bit
chmod g+s directory/ chmod 2755 directory/`Mount Points and File Systems
Understanding Mount Points
A mount point is a directory where a file system is attached to the directory tree.
Viewing Mounted File Systems
`bash
Show all mounted file systems
mountShow in human-readable format
df -hShow specific file system type
mount -t ext4Show mount options
cat /proc/mounts`Manual Mounting
`bash
Create mount point
sudo mkdir /mnt/mydiskMount device
sudo mount /dev/sdb1 /mnt/mydiskMount with specific options
sudo mount -o rw,noexec /dev/sdb1 /mnt/mydiskUnmount
sudo umount /mnt/mydisk`Permanent Mounts (/etc/fstab)
The /etc/fstab file defines file systems to be mounted at boot.
Format:
`
device mountpoint filesystem options dump pass
`
Example:
`bash
/etc/fstab
UUID=12345678-1234-1234-1234-123456789012 / ext4 defaults 0 1 UUID=87654321-4321-4321-4321-210987654321 /home ext4 defaults 0 2 /dev/sdb1 /mnt/data ext4 rw,noexec,nodev 0 0`File System Types
| Type | Description | Use Case | |------|-------------|----------| | ext4 | Fourth extended file system | Linux default | | xfs | High-performance file system | Large files, RHEL default | | btrfs | B-tree file system | Advanced features, snapshots | | ntfs | Windows NT file system | Windows compatibility | | vfat | Virtual FAT | USB drives, compatibility | | tmpfs | Temporary file system | RAM-based storage |
Best Practices
File System Navigation
1. Use absolute paths for scripts and automation 2. Use relative paths for interactive sessions 3. Always use tab completion to avoid typos 4. Be cautious with recursive operationsFile Management
1. Use descriptive file names 2. Organize files in logical directory structures 3. Regular cleanup of temporary files 4. Backup important data before modificationsSecurity Considerations
1. Follow principle of least privilege 2. Regularly audit file permissions 3. Monitor system logs in/var/log
4. Secure sensitive configuration filesSystem Maintenance
1. Monitor disk usage regularly 2. Clean up log files periodically 3. Keep track of mounted file systems 4. Document custom configurationsCommand Safety
`bash
Always verify before destructive operations
ls -la before_deletion/ rm -i important_file.txtUse safe copying
cp -i source dest # Interactive mode cp -b source dest # Backup existing filesTest commands safely
echo rm file.txt # Echo command before execution`This comprehensive guide provides a thorough understanding of the Linux file system hierarchy, essential for anyone working with Linux systems. The hierarchical structure, while initially complex, follows logical patterns that become intuitive with practice and regular use.