The Beginner's Guide to Linux File System

Master Linux file system fundamentals including FHS structure, directory hierarchy, permissions, and navigation techniques for system administration.

The Beginner's Guide to Linux File System

Introduction

The Linux file system is the backbone of every Linux distribution, serving as the organized structure that manages how data is stored, accessed, and organized on your computer. Unlike Windows with its drive letters (C:, D:, E:), Linux uses a unified hierarchical structure that begins with a single root directory and branches out into a logical tree of directories and files.

Understanding the Linux file system is crucial for anyone looking to become proficient with Linux, whether you're a system administrator, developer, or enthusiastic user. This comprehensive guide will walk you through the fundamental concepts of file hierarchy, permissions, mount points, and navigation techniques that form the foundation of Linux file system management.

Understanding the Linux File Hierarchy Standard (FHS)

The Root Directory: Where Everything Begins

In Linux, everything starts with the root directory, represented by a forward slash (/). This is not to be confused with the root user's home directory (/root). The root directory is the top-level directory from which all other directories and files branch out, creating a tree-like structure that encompasses the entire file system.

Essential System Directories

The Filesystem Hierarchy Standard (FHS) defines the structure and purpose of directories in Unix-like operating systems. Here are the most important directories you'll encounter:

/bin - Essential User Binaries This directory contains essential command-line utilities that are available to all users. Programs like ls, cp, mv, cat, and grep reside here. These are fundamental commands needed for basic system operation and recovery.

/boot - Boot Loader Files Contains static files required for the boot process, including the Linux kernel, initial RAM disk image (initrd), and bootloader configuration files. This directory is critical for system startup.

/dev - Device Files Linux treats hardware devices as files, and /dev is where these device files are located. You'll find files representing hard drives (/dev/sda, /dev/sdb), USB devices, network interfaces, and other hardware components.

/etc - Configuration Files System-wide configuration files are stored here. This includes network configuration (/etc/network/), user account information (/etc/passwd), system services configuration, and application settings that affect all users.

/home - User Home Directories Each regular user has a subdirectory under /home where their personal files, documents, and user-specific configurations are stored. For example, user "john" would have /home/john as their home directory.

/lib and /lib64 - Shared Libraries These directories contain shared library files that are essential for programs in /bin and /sbin. The /lib64 directory specifically contains 64-bit libraries on systems that support both 32-bit and 64-bit architectures.

/media and /mnt - Mount Points /media is typically used for automatically mounted removable media like USB drives and CDs, while /mnt is traditionally used for temporarily mounted file systems by system administrators.

/opt - Optional Software Third-party software packages that aren't part of the default installation are often installed here. Each package typically gets its own subdirectory under /opt.

/proc - Process Information This is a virtual file system that provides information about running processes and kernel parameters. Files here don't actually exist on disk but are generated dynamically by the kernel.

/root - Root User's Home The home directory for the root user (system administrator). This is separate from regular user home directories and is located at the root level for security and accessibility reasons.

/sbin - System Binaries Contains essential system administration programs like fdisk, fsck, and mount. These commands are typically used by the system administrator and may require elevated privileges.

/tmp - Temporary Files A directory for temporary files that may be deleted between system reboots. Applications and users can create temporary files here, but shouldn't rely on their persistence.

/usr - User Programs and Data This directory contains the majority of user utilities and applications. It has its own hierarchy with subdirectories like /usr/bin (user commands), /usr/lib (libraries), and /usr/share (shared data).

/var - Variable Data Contains files that are expected to change in size and content during normal system operation, such as log files (/var/log), mail spools (/var/mail), and temporary files that persist between reboots.

File Permissions: The Foundation of Linux Security

Understanding Permission Basics

Linux file permissions form a robust security model that controls who can access files and directories and what they can do with them. Every file and directory has three types of permissions that can be granted to three different categories of users.

The Three Permission Types

Read (r) - Permission Value 4 - For files: Allows viewing the file's contents - For directories: Allows listing the directory's contents

Write (w) - Permission Value 2 - For files: Allows modifying the file's contents - For directories: Allows creating, deleting, and renaming files within the directory

Execute (x) - Permission Value 1 - For files: Allows running the file as a program or script - For directories: Allows entering the directory and accessing its contents

The Three User Categories

Owner (User) The user who owns the file or directory, typically the person who created it.

Group Users who belong to the file's group. This allows sharing files among team members or project collaborators.

Others (World) All other users on the system who don't own the file and aren't in the file's group.

Reading Permission Notation

When you run ls -l, you'll see permissions displayed in a 10-character string. The first character indicates the file type (- for regular files, d for directories, l for symbolic links), followed by three groups of three characters each representing owner, group, and others permissions.

For example: -rwxr-xr-- - -: Regular file - rwx: Owner can read, write, and execute - r-x: Group can read and execute, but not write - r--: Others can only read

Numeric Permission Representation

Permissions can also be expressed numerically using octal notation: - Read = 4 - Write = 2 - Execute = 1

You add these values together for each user category. For example: - 755 = Owner: read+write+execute (7), Group: read+execute (5), Others: read+execute (5) - 644 = Owner: read+write (6), Group: read (4), Others: read (4) - 600 = Owner: read+write (6), Group: none (0), Others: none (0)

Changing Permissions with chmod

The chmod command modifies file permissions. You can use either symbolic or numeric notation:

Symbolic notation examples: `bash chmod u+x filename # Add execute permission for owner chmod g-w filename # Remove write permission from group chmod o=r filename # Set others to read-only chmod a+r filename # Add read permission for all (owner, group, others) `

Numeric notation examples: `bash chmod 755 filename # rwxr-xr-x chmod 644 filename # rw-r--r-- chmod 600 filename # rw------- `

Changing Ownership with chown and chgrp

chown changes file ownership: `bash chown user:group filename # Change both owner and group chown user filename # Change only owner chown :group filename # Change only group `

chgrp changes group ownership: `bash chgrp newgroup filename # Change group ownership `

Special Permissions

Setuid (SUID) - 4000 When set on executable files, the program runs with the permissions of the file owner rather than the user executing it. Displayed as 's' in the owner execute position.

Setgid (SGID) - 2000 On executable files, runs with group permissions of the file. On directories, new files inherit the directory's group. Displayed as 's' in the group execute position.

Sticky Bit - 1000 Typically used on directories like /tmp. Only the file owner can delete or rename files in the directory, even if others have write permissions. Displayed as 't' in the others execute position.

Setting special permissions: `bash chmod 4755 filename # Set SUID chmod 2755 dirname # Set SGID chmod 1755 dirname # Set sticky bit `

Understanding Mount Points and File Systems

What is Mounting?

In Linux, mounting is the process of making a file system accessible at a particular point in the directory tree. Unlike Windows, where each drive gets its own letter, Linux integrates all storage devices into a single directory hierarchy through mounting.

Common File System Types

ext4 (Fourth Extended Filesystem) The default file system for most Linux distributions. It supports large files, journaling for data integrity, and efficient performance for general-purpose use.

ext3 and ext2 Predecessors to ext4. ext3 added journaling to ext2, while ext4 improved performance and added support for larger files and volumes.

XFS A high-performance file system originally developed by Silicon Graphics. It's particularly good for large files and high-performance applications.

Btrfs (B-tree File System) A modern file system with advanced features like snapshots, compression, and built-in RAID support. Still evolving but gaining adoption.

NTFS Microsoft's file system, supported in Linux through NTFS-3G driver for compatibility with Windows partitions.

FAT32 and exFAT Older file systems still used for USB drives and SD cards due to their universal compatibility across operating systems.

The /etc/fstab File

The /etc/fstab file contains information about file systems and their mount points. It's used during boot to automatically mount file systems. Each line represents a file system with six fields:

1. Device: The device or partition to mount 2. Mount Point: Where to mount it in the directory tree 3. File System Type: The type of file system 4. Options: Mount options like read-only, permissions, etc. 5. Dump: Used by the dump utility (usually 0) 6. Pass: File system check order (0 = don't check, 1 = check first, 2 = check after 1)

Example /etc/fstab entry: ` /dev/sda1 /home ext4 defaults 0 2 `

Manual Mounting and Unmounting

Mounting a file system: `bash sudo mount /dev/sdb1 /mnt/usb # Mount USB drive sudo mount -t ntfs /dev/sda2 /mnt/windows # Mount NTFS partition `

Unmounting: `bash sudo umount /mnt/usb # Unmount by mount point sudo umount /dev/sdb1 # Unmount by device `

Viewing mounted file systems: `bash mount # Show all mounted file systems df -h # Show disk usage and mount points lsblk # Show block devices in tree format `

Mount Options

Common mount options include:

- ro: Read-only - rw: Read-write (default) - noexec: Don't allow execution of programs - nosuid: Ignore SUID and SGID bits - user: Allow ordinary users to mount - auto/noauto: Mount automatically at boot or not - defaults: Use default options (rw, suid, dev, exec, auto, nouser, async)

File System Navigation: Mastering the Command Line

Essential Navigation Commands

pwd (Print Working Directory) Shows your current location in the file system: `bash pwd

Output: /home/username/Documents

`

ls (List Directory Contents) Lists files and directories with various options: `bash ls # Basic listing ls -l # Long format with details ls -la # Include hidden files ls -lh # Human-readable file sizes ls -lt # Sort by modification time ls -lS # Sort by file size `

cd (Change Directory) Navigate between directories: `bash cd /home/username # Absolute path cd Documents # Relative path cd .. # Go up one directory cd ~ # Go to home directory cd - # Go to previous directory cd / # Go to root directory `

Path Types: Absolute vs Relative

Absolute Paths Always start with / and specify the complete path from the root directory: `bash /home/username/Documents/file.txt /etc/passwd /usr/bin/python3 `

Relative Paths Don't start with / and are relative to your current directory: `bash Documents/file.txt # file.txt in Documents subdirectory ../file.txt # file.txt in parent directory ./script.sh # script.sh in current directory `

Special Directory Symbols

- . : Current directory - .. : Parent directory - ~ : Home directory - - : Previous directory (with cd command)

Advanced Navigation Techniques

Using Tab Completion Press Tab to auto-complete file and directory names. Press Tab twice to see all possible completions.

Command History - Use arrow keys to navigate through command history - history command shows recent commands - !! repeats the last command - !n repeats command number n from history

Finding Files and Directories

find Command: `bash find /home -name "*.txt" # Find all .txt files in /home find . -type d -name "Documents" # Find directories named Documents find /var/log -mtime -7 # Files modified in last 7 days find . -size +100M # Files larger than 100MB `

locate Command: `bash locate filename # Quickly find files by name sudo updatedb # Update locate database `

which and whereis Commands: `bash which python3 # Find location of python3 executable whereis ls # Find binary, source, and manual pages `

File and Directory Operations

Creating Directories: `bash mkdir newdir # Create single directory mkdir -p path/to/nested/dirs # Create nested directories mkdir dir1 dir2 dir3 # Create multiple directories `

Copying Files and Directories: `bash cp file.txt backup.txt # Copy file cp -r directory/ backup/ # Copy directory recursively cp -p file.txt backup.txt # Preserve permissions and timestamps `

Moving and Renaming: `bash mv oldname.txt newname.txt # Rename file mv file.txt /home/user/Documents/ # Move file mv *.txt Documents/ # Move all .txt files `

Removing Files and Directories: `bash rm file.txt # Remove file rm -i file.txt # Interactive removal (ask confirmation) rm -r directory/ # Remove directory recursively rm -rf directory/ # Force remove without confirmation `

File Content Operations

Viewing File Contents: `bash cat file.txt # Display entire file less file.txt # View file page by page head file.txt # Show first 10 lines tail file.txt # Show last 10 lines tail -f /var/log/syslog # Follow log file in real-time `

Searching Within Files: `bash grep "pattern" file.txt # Search for pattern in file grep -r "pattern" directory/ # Recursive search in directory grep -i "pattern" file.txt # Case-insensitive search grep -n "pattern" file.txt # Show line numbers `

Working with Links

Hard Links: `bash ln file.txt hardlink.txt # Create hard link `

Symbolic (Soft) Links: `bash ln -s /path/to/file symlink # Create symbolic link ln -s /path/to/directory dirlink # Link to directory `

Advanced File System Concepts

File System Monitoring and Maintenance

Checking Disk Usage: `bash df -h # Show disk space usage du -h directory/ # Show directory size du -sh * # Show size of all items in current directory `

File System Checking: `bash sudo fsck /dev/sda1 # Check file system integrity sudo fsck -f /dev/sda1 # Force check even if clean `

Understanding Inodes

Every file and directory in Linux has an inode (index node) that contains metadata about the file, including: - File size - Owner and group - Permissions - Timestamps (creation, modification, access) - Location of data blocks

`bash ls -i file.txt # Show inode number stat file.txt # Show detailed inode information `

File System Security Best Practices

1. Regular Permission Audits: Periodically review file permissions, especially for sensitive files 2. Proper Mount Options: Use restrictive mount options for external devices 3. Monitor Disk Usage: Keep an eye on disk space to prevent system issues 4. Backup Important Data: Regular backups protect against file system corruption 5. Use Appropriate File Systems: Choose file systems based on your specific needs

Troubleshooting Common File System Issues

Permission Denied Errors

When you encounter "Permission denied" errors: 1. Check file permissions with ls -l 2. Verify you're the owner or in the correct group 3. Use sudo for administrative tasks 4. Check if the file system is mounted read-only

Disk Space Issues

When running out of disk space: 1. Use df -h to identify full partitions 2. Use du -sh /* to find large directories 3. Clean temporary files in /tmp and /var/tmp 4. Remove old log files from /var/log 5. Clear package caches if applicable

Mount Issues

Common mounting problems: 1. Device busy: Ensure no processes are using the mount point 2. Wrong file system type: Specify the correct type with -t 3. Permission issues: Check if user mounting is allowed 4. Corrupted file system: Run fsck to check and repair

Conclusion

Understanding the Linux file system is fundamental to becoming proficient with Linux. The hierarchical structure, starting from the root directory and branching into standardized directories, provides a logical organization that, once learned, makes navigation intuitive and efficient.

The permission system offers robust security through its three-tiered approach of owner, group, and others permissions, each with read, write, and execute capabilities. Mastering these concepts allows you to properly secure your files and understand access control in multi-user environments.

Mount points and file systems provide the flexibility to integrate various storage devices and file system types into a unified directory tree. This understanding is crucial for system administration and managing storage effectively.

Navigation skills, from basic commands like ls, cd, and pwd to advanced techniques using find, grep, and various file operations, form the toolkit you'll use daily when working with Linux systems.

As you continue your Linux journey, these fundamental concepts will serve as the foundation for more advanced topics like system administration, shell scripting, and server management. Practice these commands and concepts regularly, and you'll find yourself navigating and managing Linux file systems with confidence and efficiency.

Remember that the Linux file system is designed with logic and consistency in mind. While it may seem complex at first, the standardized structure and predictable behavior make it a powerful and reliable foundation for computing tasks ranging from personal desktop use to enterprise server management.

Tags

  • Linux
  • Unix
  • fhs
  • file-system
  • system-administration

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

The Beginner's Guide to Linux File System