Linux Filesystem

Linux Filesystem

This section covers the Linux filesystem hierarchy, important directories, and filesystem-related commands.

Linux Filesystem Hierarchy

The Linux filesystem follows the Filesystem Hierarchy Standard (FHS), which defines the directory structure and directory contents.

Important Directories

Root Directory (/)

The root directory is the top-level directory in the Linux filesystem hierarchy. All other directories are subdirectories of the root, either directly or indirectly. When referenced in paths, the root directory is denoted by a single forward slash /.

/bin

Contains essential command binaries (programs) that are needed in single-user mode and by all users of the system.

Key Contents:

/boot

Contains files needed to boot the system.

Key Contents:

/dev

Contains device files that represent physical and virtual devices on the system.

Key Contents:

/etc

Contains system-wide configuration files and shell scripts used during system boot.

Key Contents:

/home

Contains user home directories. Each user typically has a subdirectory named after their username.

Key Contents:

/lib and /lib64

Contains essential shared libraries and kernel modules needed to boot the system and run the commands in /bin and /sbin.

Key Contents:

/media

Mount point for removable media such as USB drives, CD-ROMs, etc.

Key Contents:

/mnt

Temporary mount point for manually mounted filesystems.

Usage:

/opt

Contains optional software packages and add-on applications.

Usage:

/proc

A virtual filesystem that provides information about processes and system information.

Key Contents:

/root

Home directory for the root user. Similar to directories in /home/ but specifically for the superuser.

/run

Contains runtime data for processes started since the last boot. This data is typically cleared at boot time.

Key Contents:

/sbin

Contains system binaries (commands) essential for system administration. These commands are typically used by the system administrator.

Key Contents:

/srv

Contains data for services provided by the system, such as web servers, FTP servers, etc.

Example Structure:

/sys

A virtual filesystem that provides a view into the kernel's device tree. It exposes information about devices, drivers, and some kernel features.

Key Contents:

/tmp

Contains temporary files created by system and users. These files may be deleted when the system is rebooted.

Usage:

/usr

Secondary hierarchy containing user binaries, libraries, documentation, and source code. This directory contains non-essential files for multi-user functionality.

Key Contents:

/var

Contains variable data files. This includes logs, spool files, temporary e-mail files, and other files that change during the normal operation of the system.

Key Contents:

Important Files

| File | Description | |----------------------------------------------------|-------------------------------------------------------| | /etc/passwd | User account information | | /etc/shadow | Secure user account information (encrypted passwords) | | /etc/group | Group definitions | | /etc/sudoers | Sudo configuration | | /etc/fstab | Filesystem table (mount points) | | /etc/hosts | Static hostname to IP mappings | | /etc/resolv.conf | DNS resolver configuration | | /etc/hostname | System hostname | | /etc/network/interfaces or /etc/netplan/*.yaml | Network configuration | | /etc/ssh/sshd_config | SSH server configuration | | /var/log/syslog or /var/log/messages | System logs | | /var/log/auth.log or /var/log/secure | Authentication logs | | /boot/grub/grub.cfg or /boot/grub2/grub.cfg | GRUB bootloader configuration | | /etc/crontab | System-wide cron jobs |

Filesystem Types

Linux supports various filesystem types:

| Filesystem | Description | |------------|-----------------------------------------------------------------------------------| | ext4 | Fourth Extended Filesystem (default on many Linux distributions) | | xfs | High-performance journaling filesystem (default on some enterprise distributions) | | btrfs | B-tree filesystem with advanced features like snapshots and RAID | | f2fs | Flash-Friendly File System (optimized for SSDs) | | zfs | Advanced filesystem with volume management capabilities | | tmpfs | In-memory filesystem for temporary files | | vfat | Compatible with Windows FAT32 (often used on USB drives) | | ntfs | Windows NTFS filesystem (read/write support via NTFS-3G) | | exfat | Extended FAT filesystem (for large files on external media) | | nfs | Network File System for accessing files over a network | | cifs/smb | Common Internet File System/Server Message Block (for Windows shares) |

Filesystem Management Commands

df

Displays disk space usage for mounted filesystems.

df [options] [filesystem]

Options:

Examples:

# Show disk space usage in human-readable format
df -h

# Show filesystem types along with usage
df -hT

# Show inode usage
df -i

du

Estimates file and directory space usage.

du [options] [file or directory]

Options:

Examples:

# Show disk usage for a directory
du -h /var/log

# Show total size only
du -sh /home/user

# Show usage for each file/directory with a grand total
du -ach /etc

mount

Mounts a filesystem.

mount [options] [device] [mountpoint]

Options:

Examples:

# Mount a USB drive
sudo mount /dev/sdb1 /mnt/usb

# Mount with specific filesystem type
sudo mount -t ext4 /dev/sda2 /mnt/data

# Mount with options
sudo mount -o ro,noexec /dev/sdb1 /mnt/usb

# Mount all filesystems in fstab
sudo mount -a

umount

Unmounts a filesystem.

umount [options] [device or mountpoint]

Options:

Examples:

# Unmount by mountpoint
sudo umount /mnt/usb

# Unmount by device
sudo umount /dev/sdb1

# Force unmount
sudo umount -f /mnt/usb

# Lazy unmount (useful when device is busy)
sudo umount -l /mnt/usb

lsblk

Lists information about block devices.

lsblk [options]

Options:

Examples:

# List all block devices
lsblk

# Show filesystem information
lsblk -f

# Show custom columns
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT,UUID

blkid

Prints block device attributes.

blkid [options] [device]

Options:

Examples:

# Show all block devices with attributes
sudo blkid

# Find device by UUID
sudo blkid -U "a1b2c3d4-e5f6-7890-abcd-1234567890ab"

# Find device by label
sudo blkid -L "SYSTEM"

fdisk

Manipulates disk partition tables.

fdisk [options] [device]

Options:

Examples:

# List all partition tables
sudo fdisk -l

# Manipulate partitions on a specific disk
sudo fdisk /dev/sdb

mkfs

Creates a filesystem on a device.

mkfs [options] -t [fstype] [device]

Examples:

# Create an ext4 filesystem
sudo mkfs -t ext4 /dev/sdb1

# Create an XFS filesystem
sudo mkfs.xfs /dev/sdc1

# Create a FAT32 filesystem
sudo mkfs.vfat -F 32 /dev/sdd1

fsck

Checks and repairs a filesystem.

fsck [options] [filesystem]

Options:

Examples:

# Check the root filesystem at next boot
sudo touch /forcefsck

# Check a specific filesystem
sudo fsck /dev/sdb1

# Force check and automatically repair
sudo fsck -f -y /dev/sdb1

findmnt

Lists mounted filesystems or searches for a filesystem.

findmnt [options] [device|mountpoint]

Options:

Examples:

# List all mounted filesystems
findmnt

# Find all ext4 filesystems
findmnt -t ext4

# Find filesystem mounted at specific point
findmnt /home

# Find mounts from specific device
findmnt -S /dev/sda1

tune2fs

Adjusts parameters on ext2/ext3/ext4 filesystems.

tune2fs [options] device

Options:

Examples:

# Show filesystem parameters
sudo tune2fs -l /dev/sda1

# Set filesystem label
sudo tune2fs -L "DATA" /dev/sdb1

# Disable automatic fsck at boot (not recommended)
sudo tune2fs -c 0 -i 0 /dev/sda1

e2label

Changes the label on an ext2/ext3/ext4 filesystem.

e2label device [label]

Examples:

# Show current label
sudo e2label /dev/sda1

# Set a new label
sudo e2label /dev/sda1 "ROOT"

fstab

The /etc/fstab file contains static filesystem information. Each filesystem is described on a separate line with fields separated by whitespace.

Format:

[device] [mountpoint] [filesystem] [options] [dump] [pass]

Example /etc/fstab entry:

#                                   
UUID=123abc...    /               ext4     errors=remount-ro               0       1
/dev/sdb1         /home           ext4     defaults                        0       2
/dev/sdc1         /media/data     xfs      defaults,noatime                0       2
//server/share    /mnt/nas        cifs     credentials=/etc/samba/creds    0       0
tmpfs             /tmp            tmpfs    defaults,size=2G,noexec         0       0

Fields:

  1. . Device: Device name, UUID, or label
  2. . Mount point: Directory where the filesystem is mounted
  3. . Filesystem type: ext4, xfs, ntfs, etc.
  4. . Mount options: Comma-separated list of options
  5. . Dump: Used by dump utility (0 = don't dump, 1 = dump)
  6. . Pass: Order in which fsck checks filesystems (0 = don't check, 1 = root, 2 = other)