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
Directory | Purpose |
---|---|
/bin | Essential command binaries (programs) that need to be available for all users |
/boot | Boot loader files, kernel images, and initial ramdisk files |
/dev | Device files that represent hardware devices |
/etc | System-wide configuration files |
/home | User home directories |
/lib | Essential shared libraries and kernel modules |
/media | Mount points for removable media (USB drives, CDs, etc.) |
/mnt | Mount point for temporarily mounted filesystems |
/opt | Optional application software packages |
/proc | Virtual filesystem that provides process and kernel information |
/root | Home directory for the root user |
/sbin | Essential system binaries used for system administration |
/srv | Data for services provided by the system |
/sys | Virtual filesystem for kernel and device information (similar to /proc) |
/tmp | Temporary files that are typically cleared on reboot |
/usr | Secondary hierarchy for user data and applications |
/var | Variable 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:
- Regular files: Normal files containing data, text, or programs
- Directories: Special files that contain listings of other files
- Links:
- Hard links: Additional references to the same inode (data)
- Symbolic links: Pointers to other files or directories
- 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)
- Sockets: Enable inter-process communication
- 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:
- ext4: The default filesystem for many Linux distributions
- XFS: High-performance filesystem, good for large files
- Btrfs: Modern filesystem with advanced features like snapshots
- NTFS/FAT32: Windows compatibility filesystems
- NFS: Network File System for remote file access
Practice Exercises
- Navigate through the filesystem and identify the purpose of different directories
- Create a directory structure for a project with multiple subdirectories
- Use relative and absolute paths to navigate between directories
- Find all files larger than 100MB in your home directory
- 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.