Table of Contents
- Understanding the Linux File System Hierarchy
- 1.1 The Root Directory (
/) - 1.2 Key Subdirectories Explained
- 1.1 The Root Directory (
- File System Types: Choosing the Right One
- 2.1 ext4: The Default Workhorse
- 2.2 XFS: High Performance for Large Files
- 2.3 Btrfs: Advanced Features (Snapshots, RAID)
- 2.4 ZFS: Enterprise-Grade Reliability
- 2.5 Other File Systems (tmpfs, NFS, NTFS)
- Essential File System Commands
- 3.1 Navigating the File System
- 3.2 Creating, Copying, Moving, and Deleting Files
- 3.3 Managing File Permissions and Ownership
- 3.4 Viewing and Editing File Contents
- Mounting and Unmounting File Systems
- 4.1 What is Mounting?
- 4.2 The
mountandumountCommands - 4.3 Persistent Mounting with
/etc/fstab - 4.4 Mounting Removable Devices (USB, External Drives)
- Disk Management: Partitioning and Formatting
- 5.1 Identifying Disks and Partitions (
lsblk,fdisk -l) - 5.2 Partitioning with
fdiskandparted - 5.3 Formatting with
mkfs
- 5.1 Identifying Disks and Partitions (
- Advanced Topics: LVM, RAID, and Snapshots
- 6.1 Logical Volume Management (LVM): Flexibility in Storage
- 6.2 RAID: Redundancy and Performance
- 6.3 Snapshots: Protecting Data with Point-in-Time Backups
- Troubleshooting Common File System Issues
- 7.1 “File System is Read-Only” Errors
- 7.2 “Disk Full” Scenarios
- 7.3 Permission Denied: Diagnosing Access Issues
- 7.4 Repairing Corrupted File Systems with
fsck
- Best Practices for File System Management
- Conclusion
- References
1. Understanding the Linux File System Hierarchy
Linux uses a single-rooted, hierarchical file system—all files and directories stem from the root directory (/), regardless of the number of physical storage devices (hard drives, SSDs, USBs) attached to the system. This structure ensures consistency across all Linux distributions (e.g., Ubuntu, Fedora, Debian).
1.1 The Root Directory (/)
The root directory (/) is the topmost level of the hierarchy. All other directories and files branch out from here. Unlike Windows (which uses drive letters like C: or D:), Linux treats all storage devices as part of this single tree.
1.2 Key Subdirectories Explained
Below are the most critical subdirectories under / and their purposes:
| Directory | Purpose |
|---|---|
/bin | Essential binary executables (e.g., ls, cp, mv) needed for booting. |
/boot | Bootloader files, kernel images, and initramfs (e.g., vmlinuz, grub). |
/dev | Device files (e.g., /dev/sda for the first hard drive, /dev/null for null device). |
/etc | System-wide configuration files (e.g., passwd, fstab, nginx.conf). |
/home | User-specific directories (e.g., /home/alice, /home/bob). |
/lib//lib64 | Shared libraries required by binaries in /bin and /sbin. |
/media | Mount points for removable media (e.g., USB drives, CDs). |
/mnt | Temporary mount points for additional storage (e.g., network shares). |
/opt | Optional software (e.g., third-party apps like Chrome, Docker). |
/proc | Virtual file system exposing kernel and process information (e.g., /proc/cpuinfo). |
/root | Home directory for the root (administrative) user. |
/sbin | System binaries for admin tasks (e.g., fdisk, mount, reboot). |
/srv | Data for services (e.g., /srv/www for a web server). |
/sys | Virtual file system for hardware and kernel device information. |
/tmp | Temporary files (cleared on reboot; world-writable). |
/usr | User utilities and applications (e.g., /usr/bin for user binaries, /usr/share for shared data). |
/var | Variable data (e.g., logs in /var/log, spool files in /var/spool). |
2. File System Types: Choosing the Right One
Linux supports dozens of file system types, each optimized for specific use cases. Below are the most common:
2.1 ext4: The Default Workhorse
- Overview: The fourth extended file system (
ext4) is the default for most Linux distributions (Ubuntu, Fedora, Debian). It’s a mature, stable successor toext3. - Features: Supports files up to 16 TiB, volumes up to 1 EiB, journaling (for crash recovery), and backward compatibility with
ext3/ext2. - Use Case: General-purpose storage (desktops, laptops, servers).
2.2 XFS: High Performance for Large Files
- Overview: Developed by SGI, XFS is a high-performance, 64-bit journaling file system.
- Features: Optimized for large files (e.g., video, databases), parallel I/O (faster on multi-core systems), and online resizing.
- Use Case: Enterprise servers, media editing workstations, or systems handling large datasets.
2.3 Btrfs: Advanced Features (Snapshots, RAID)
- Overview: Btrfs (B-tree File System) is a modern, copy-on-write (CoW) file system designed for flexibility.
- Features: Built-in RAID (0, 1, 5, 6), snapshots, subvolumes, online resizing, and checksumming (for data integrity).
- Use Case: Systems needing snapshots (e.g., development environments), or those requiring RAID without external tools.
2.4 ZFS: Enterprise-Grade Reliability
- Overview: Originally developed by Sun Microsystems, ZFS is a combined file system and volume manager.
- Features: End-to-end checksumming, RAID-Z (improved RAID), snapshots, deduplication, and compression.
- Use Case: Enterprise storage, backup servers, or systems prioritizing data integrity (e.g., financial databases).
- Note: ZFS is not included in the mainline Linux kernel (due to licensing), but is available via third-party modules (e.g., OpenZFS).
2.5 Other File Systems
- tmpfs: A temporary file system stored in RAM (e.g.,
/tmpon some systems). Fast but volatile (data lost on reboot). - NFS: Network File System for sharing files over a network.
- NTFS: Windows file system; Linux can read/write to NTFS via
ntfs-3gdriver.
3. Essential File System Commands
Mastering basic commands is the first step to managing Linux file systems. Below are must-know operations:
3.1 Navigating the File System
-
pwd: Print the current working directory.pwd # Output: /home/alice/documents -
cd: Change directory.cd /var/log # Go to /var/log cd .. # Move up one directory cd ~ # Go to your home directory cd - # Go back to the previous directory -
ls: List directory contents.ls -l # Long format (permissions, size, date) ls -a # Show hidden files (starts with .) ls -lh # Human-readable sizes (e.g., 1K, 2M) ls -t # Sort by modification time (newest first)
3.2 Creating, Copying, Moving, and Deleting Files
-
touch: Create an empty file or update timestamps.touch notes.txt # Create notes.txt touch -d "2023-01-01" old_file # Set timestamp to Jan 1, 2023 -
mkdir: Create directories.mkdir projects # Create a single directory mkdir -p photos/2023/june # Create nested directories (-p = parent) -
cp: Copy files/directories.cp file1.txt file2.txt # Copy file1.txt to file2.txt cp -r docs/ backups/ # Copy directory (recursive, -r) cp -v file.txt /tmp/ # Verbose mode (show progress) -
mv: Move or rename files/directories.mv oldname.txt newname.txt # Rename a file mv report.pdf ~/documents/ # Move report.pdf to home/documents -
rm: Delete files.rm unwanted.txt # Delete a file rm -f stubborn.txt # Force delete (no prompts) rm -r old_dir/ # Delete a directory (recursive) rm -rf * # CAUTION: Delete all files in current directory (irreversible!) -
rmdir: Delete empty directories (userm -rfor non-empty ones).
3.3 Managing File Permissions and Ownership
Linux uses a permission system to control access to files/directories. Permissions are divided into three categories:
- User (u): The file owner.
- Group (g): Users in the file’s group.
- Others (o): All other users.
Each category has three permissions: read (r), write (w), and execute (x).
Viewing Permissions with ls -l
ls -l file.txt
# Output: -rw-r--r-- 1 alice users 1024 Jun 1 12:00 file.txt
- The first character:
-(file),d(directory), orl(symlink). - Next 9 characters: Permissions for user (rw-), group (r—), others (r—).
Changing Permissions with chmod
Use symbolic mode (e.g., u+x, g-w) or numeric mode (e.g., 755, 644).
Symbolic Mode:
chmod u+x script.sh # Add execute permission for the owner
chmod g-w file.txt # Remove write permission for the group
chmod o=r file.txt # Set "read-only" for others
chmod a+rwx file.txt # Add read/write/execute for all (a = all)
Numeric Mode: Permissions are represented as a 3-digit number (user, group, others), where:
r = 4,w = 2,x = 1,no permission = 0.
Examples:
chmod 755 script.sh # u=rwx (7), g=rx (5), o=rx (5)
chmod 644 file.txt # u=rw (6), g=r (4), o=r (4)
chmod 700 secret/ # Only owner can read/write/execute
Changing Ownership with chown and chgrp
-
chown: Change the owner (and group).chown bob file.txt # Change owner to "bob" chown alice:users data.csv # Change owner to "alice" and group to "users" chown -R root:/var/log /var/log # Recursively change owner/group of /var/log -
chgrp: Change only the group.chgrp developers project/ # Change group of "project/" to "developers"
3.4 Viewing and Editing File Contents
-
cat: Display file contents.cat notes.txt -
less/more: View large files interactively (use arrow keys to scroll,qto quit).less /var/log/syslog -
head/tail: Show the first/last N lines of a file.head -5 report.txt # First 5 lines tail -f /var/log/auth.log # "Follow" a log file (updates in real time)
4. Mounting and Unmounting File Systems
In Linux, storage devices (e.g., hard drives, USBs) are not directly accessible until they are mounted—attached to a directory in the file system tree (the “mount point”).
4.1 What is Mounting?
A mount point is an empty directory where the file system of a storage device is attached. For example, a USB drive with an ext4 file system might be mounted at /media/alice/usb-drive.
4.2 The mount and umount Commands
-
mount: List all mounted file systems (without arguments) or mount a device.mount # List all mounted file systems mount /dev/sdb1 /mnt/usb # Mount /dev/sdb1 (USB drive) to /mnt/usb -
umount: Unmount a file system (ensure no processes are using it first!).umount /mnt/usb # Unmount /mnt/usb umount /dev/sdb1 # Alternatively, unmount by device
4.3 Persistent Mounting with /etc/fstab
To mount a file system automatically at boot, add an entry to /etc/fstab (file system table). The format is:
<device> <mount-point> <file-system-type> <options> <dump> <pass>
Example: Mount an ext4 partition (/dev/sdb1) at /data on boot:
# Edit /etc/fstab with sudo
sudo nano /etc/fstab
# Add this line:
/dev/sdb1 /data ext4 defaults 0 2
-
<device>: Can be a device path (/dev/sdb1), UUID (more reliable), or label.
To find a device’s UUID:blkid /dev/sdb1 # Output: /dev/sdb1: UUID="a1b2c3d4..." TYPE="ext4"Replace
/dev/sdb1withUUID=a1b2c3d4...in/etc/fstabfor stability. -
<options>:defaults(rw, suid, dev, exec, auto, nouser, async). -
<dump>:0(no backup) or1(backup withdumptool). -
<pass>:0(no fsck),1(check first at boot, for root),2(check after root).
4.4 Mounting Removable Devices
Most Linux desktops (GNOME, KDE) auto-mount USB drives to /media/<user>/<label>. For CLI mounting:
sudo mount /dev/sdb1 /media/usb # Mount USB drive
sudo umount /media/usb # Unmount when done
5. Disk Management: Partitioning and Formatting
Before a storage device (HDD, SSD) can be used, it must be partitioned (divided into logical sections) and formatted (assigned a file system).
5.1 Identifying Disks and Partitions
Use these commands to list storage devices and their partitions:
-
lsblk: List block devices (disks and partitions).lsblk # Output: NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT # sda 8:0 0 238.5G 0 disk # ├─sda1 8:1 0 512M 0 part /boot/efi # └─sda2 8:2 0 238G 0 part / # sdb 8:16 0 1.8T 0 disk # └─sdb1 8:17 0 1.8T 0 part /data -
fdisk -l: Detailed disk/partition info (run as root).sudo fdisk -l /dev/sdb # Info about disk /dev/sdb
5.2 Partitioning with fdisk and parted
-
fdisk: For MBR (Master Boot Record) partitions (supports disks < 2 TiB).sudo fdisk /dev/sdb # Launch fdisk for disk /dev/sdbUse
nto create a new partition,pto list partitions,wto write changes, andqto quit. -
parted: For GPT (GUID Partition Table) partitions (supports disks > 2 TiB).sudo parted /dev/sdb # Launch parted for disk /dev/sdb (parted) mklabel gpt # Set partition table to GPT (parted) mkpart primary ext4 0% 100% # Create a single ext4 partition (0% to 100% of disk) (parted) quit
5.3 Formatting with mkfs
After partitioning, format the partition with a file system using mkfs.<type>:
sudo mkfs.ext4 /dev/sdb1 # Format /dev/sdb1 as ext4
sudo mkfs.xfs /dev/sdc1 # Format /dev/sdc1 as XFS
sudo mkfs.btrfs /dev/sdd1 # Format /dev/sdd1 as Btrfs
6. Advanced Topics: LVM, RAID, and Snapshots
6.1 Logical Volume Management (LVM): Flexibility in Storage
LVM abstracts physical storage (disks/partitions) into volume groups (VGs), which are divided into logical volumes (LVs) (the “virtual partitions” you mount).
Benefits: Resize LVs on the fly, combine multiple disks into a single volume, and create snapshots.
Basic LVM Workflow:
-
Create physical volumes (PVs): Initialize disks/partitions for LVM.
sudo pvcreate /dev/sdb1 /dev/sdc1 # Initialize /dev/sdb1 and /dev/sdc1 as PVs -
Create a volume group (VG): Combine PVs into a pool of storage.
sudo vgcreate my_vg /dev/sdb1 /dev/sdc1 # Create VG "my_vg" -
Create logical volumes (LVs): Allocate space from the VG.
sudo lvcreate -L 100G -n my_lv my_vg # Create 100GB LV "my_lv" in "my_vg" -
Format and mount the LV:
sudo mkfs.ext4 /dev/my_vg/my_lv sudo mount /dev/my_vg/my_lv /data -
Resize an LV (e.g., add 50GB to
my_lv):sudo lvextend -L +50G /dev/my_vg/my_lv # Extend LV sudo resize2fs /dev/my_vg/my_lv # Resize ext4 file system (for XFS: xfs_growfs)
6.2 RAID: Redundancy and Performance
RAID (Redundant Array of Independent Disks) combines multiple disks to improve performance or protect against data loss. Common RAID levels:
- RAID 0: Striping (no redundancy). Faster read/write, but data lost if one disk fails.
- RAID 1: Mirroring (100% redundancy). Data duplicated across disks; survives one disk failure.
- RAID 5: Striping with parity (requires ≥3 disks). Survives one disk failure; good balance of speed and redundancy.
- RAID 6: Striping with double parity (requires ≥4 disks). Survives two disk failures.
Tools: Use mdadm (software RAID) or hardware RAID controllers.
Example: Create RAID 1 with mdadm:
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
sudo mkfs.ext4 /dev/md0
sudo mount /dev/md0 /mnt/raid1
6.3 Snapshots: Protecting Data with Point-in-Time Backups
Snapshots capture the state of a file system at a specific time, allowing you to restore data later.
-
Btrfs Snapshots: Built into Btrfs.
# Create a read-only snapshot of /data sudo btrfs subvolume snapshot -r /data /data/snapshots/2023-06-01 # Restore from snapshot sudo mv /data /data_old sudo btrfs subvolume snapshot /data/snapshots/2023-06-01 /data -
LVM Snapshots: Create a temporary LV to capture the state of an existing LV.
sudo lvcreate -L 10G -s -n my_lv_snap /dev/my_vg/my_lv # Create 10GB snapshot
7. Troubleshooting Common File System Issues
7.1 “File System is Read-Only” Errors
Causes: Disk errors, unclean shutdown, or ro mount option.
Fix:
- Check
dmesgfor errors:dmesg | grep "read-only" - Remount as read-write (if safe):
sudo mount -o remount,rw / - Run
fsck(file system check) on the unmounted device:sudo umount /dev/sdb1 sudo fsck.ext4 /dev/sdb1 # Replace ext4 with your file system type
7.2 “Disk Full” Scenarios
Use df and du to identify large files:
df -h # Check disk usage (human-readable)
du -sh /home/* # Find large directories in /home
Fix: Delete unnecessary files, move data to another disk, or resize the file system.
7.3 Permission Denied: Diagnosing Access Issues
- Check ownership/permissions with
ls -l. - Ensure the user has
xpermission on parent directories (e.g., to access/home/alice/file.txt,/homeand/home/aliceneedx).
7.4 Repairing Corrupted File Systems with fsck
fsck (file system check) repairs corrupted file systems. Always unmount the device first!
sudo umount /dev/sdb1
sudo fsck -y /dev/sdb1 # -y: automatically fix errors
8. Best Practices for File System Management
- Organize Files Logically: Use consistent directory structures (e.g.,
/data/projects,/backup/weekly). - Regular Backups: Use tools like
rsync,borgbackup, or LVM/Btrfs snapshots to protect data. - Monitor Disk Space: Use
df -hor tools likencdu(ncurses disk usage) to track growth. - Limit Root Access: Avoid using
rootfor daily tasks; usesudofor admin actions. - Secure Sensitive Files: Restrict permissions (e.g.,
chmod 600 ~/.ssh/id_rsafor SSH keys). - Document Changes: Keep a log of partition/mount modifications (e.g., edits to
/etc/fstab).
Conclusion
Linux file system management is a foundational skill for anyone working with Linux, from casual users to system administrators. By mastering the hierarchy, commands, and advanced tools covered here, you’ll be able to organize, secure, and maintain your data with confidence. Remember: practice makes perfect—experiment with commands in a safe environment (e.g., a VM) to build proficiency.