Skip to content

Understanding the Linux File System

Linux organizes files in a hierarchical directory structure, often referred to as the Filesystem Hierarchy Standard (FHS). This structure starts from the root directory, represented by a forward slash /.

Directory Structure

Root Directory (/)

The root directory is the top-level directory in the Linux filesystem hierarchy. All other files and directories are descendants of this directory.

Key System Directories

DirectoryPurpose
/binEssential command binaries (programs) that need to be available for all users
/bootBoot loader files, kernel images, and initial ramdisk files
/devDevice files that represent hardware devices
/etcSystem-wide configuration files
/homeUser home directories
/libEssential shared libraries and kernel modules
/mediaMount points for removable media (USB drives, CDs, etc.)
/mntMount point for temporarily mounted filesystems
/optOptional application software packages
/procVirtual filesystem that provides process and kernel information
/rootHome directory for the root user
/sbinEssential system binaries used for system administration
/srvData for services provided by the system
/sysVirtual filesystem for kernel and device information (similar to /proc)
/tmpTemporary files that are typically cleared on reboot
/usrSecondary hierarchy for user data and applications
/varVariable data files (logs, spool files, temporary files, etc.)

Understanding Paths

Absolute vs. Relative Paths

  • Absolute path: Starts from the root directory (/), e.g., /home/user/documents/file.txt
  • Relative path: Relative to the current directory, e.g., documents/file.txt

Path Shortcuts

  • . (dot): References the current directory
  • .. (double dot): References the parent directory
  • ~ (tilde): References the home directory of the current user

File Types in Linux

Linux treats everything as a file, but there are different types:

  1. Regular files: Normal files containing data, text, or programs
  2. Directories: Special files that contain listings of other files
  3. Links:
    • Hard links: Additional references to the same inode (data)
    • Symbolic links: Pointers to other files or directories
  4. Device files:
    • Block devices: Storage devices that can be accessed in blocks (e.g., hard drives)
    • Character devices: Devices that handle data as character streams (e.g., terminals)
  5. Sockets: Enable inter-process communication
  6. Named pipes: Allow processes to communicate with each other

Viewing File Information

Using ls -l

When you run ls -l, you'll see output like:

-rwxr-xr-x 1 user group 5096 Jun 10 14:30 example.sh

This format provides:

  • File permissions (-rwxr-xr-x)
  • Number of links (1)
  • Owner name (user)
  • Group name (group)
  • File size (5096 bytes)
  • Last modification time (Jun 10 14:30)
  • File name (example.sh)

Finding File Information

bash
# Get detailed file information
stat filename

# Find files by name
find /path/to/search -name "filename"

# Find files by type
find /path/to/search -type f  # f for regular files
find /path/to/search -type d  # d for directories

# Find files by size
find /path/to/search -size +10M  # Files larger than 10MB

Disk Usage and Management

Checking Disk Space

bash
# Show disk usage for all mounted filesystems
df -h

# Show disk usage for a specific directory
du -sh /path/to/directory

# Show disk usage for all subdirectories in current directory
du -sh */

Mounting and Unmounting Filesystems

bash
# Mount a filesystem
sudo mount /dev/sdb1 /mnt/usb

# Unmount a filesystem
sudo umount /mnt/usb

# List all mounted filesystems
mount

File System Types

Linux supports various filesystem types:

  1. ext4: The default filesystem for many Linux distributions
  2. XFS: High-performance filesystem, good for large files
  3. Btrfs: Modern filesystem with advanced features like snapshots
  4. NTFS/FAT32: Windows compatibility filesystems
  5. NFS: Network File System for remote file access

Practice Exercises

  1. Navigate through the filesystem and identify the purpose of different directories
  2. Create a directory structure for a project with multiple subdirectories
  3. Use relative and absolute paths to navigate between directories
  4. Find all files larger than 100MB in your home directory
  5. Check which filesystem is using the most disk space on your system

In the next section, we'll explore Linux permissions and how to manage them.