1. File System Overview
A file system is the way an operating system organizes and stores data. It defines how files are stored, named, accessed, and managed. In Linux, the file system is a hierarchical directory structure starting from the root directory (/) and extending downward with various subdirectories.
1.1 Features of Linux File System
- Hierarchical Structure: Linux uses a tree-like directory structure, extending downward from the root directory.
- Everything is a File: In Linux, almost everything is treated as a file, including devices, inter-process communication mechanisms, etc.
- File Permissions: Linux implements fine-grained file permission control to ensure file security.
- Multiple File System Support: Linux supports multiple file system types, such as ext4, XFS, Btrfs, etc.
- Mount Mechanism: Linux uses a mount mechanism to integrate different store devices into a unified file system tree.
2. Linux File System Hierarchy
Linux follows the File System Hierarchy Standard (FHS), which defines standards for directory structure and content. Here are the purposes of the main directories:
2.1 Main Directories and Their Purposes
- /: Root directory, the starting point for all files and directories.
- /bin: Contains basic command binary files, such as ls, cp, mv, etc.
- /sbin: Contains system administration command binary files, such as fdisk, ifconfig, etc.
- /etc: Contains system configuration files, such as network configuration, user account configuration, etc.
- /home: User home directories, each user has their own subdirectory here.
- /root: Root user's home directory.
- /var: Contains variable files, such as logs, mail, cache, etc.
- /tmp: Temporary file directory, contents are cleared after system reboot.
- /usr: Contains user programs and data, such as applications, library files, etc.
- /lib: Contains system library files, such as shared libraries.
- /mnt: Temporary mount points, used for mounting external devices.
- /media: Automatic mount points, used for mounting removable media.
- /dev: Contains device files, such as hard drives, optical drives, etc.
- /proc: Virtual file system, contains system and process information.
- /sys: Virtual file system, contains hardware device information.
2.2 Directory Structure Example
$ ls -la / total 64 drwxr-xr-x 20 root root 4096 Apr 10 14:23 . drwxr-xr-x 20 root root 4096 Apr 10 14:23 .. drwxr-xr-x 2 root root 4096 Apr 10 14:24 bin drwxr-xr-x 3 root root 4096 Apr 10 14:25 boot drwxr-xr-x 18 root root 4608 Apr 10 14:26 dev drwxr-xr-x 141 root root 4096 Apr 10 14:27 etc drwxr-xr-x 4 root root 4096 Apr 10 14:28 home drwxr-xr-x 14 root root 4096 Apr 10 14:24 lib drwxr-xr-x 2 root root 4096 Apr 10 14:23 media drwxr-xr-x 2 root root 4096 Apr 10 14:23 mnt drwxr-xr-x 3 root root 4096 Apr 10 14:29 opt dr-xr-xr-x 330 root root 0 Apr 10 14:26 proc drwx------ 6 root root 4096 Apr 10 14:30 root drwxr-xr-x 2 root root 4096 Apr 10 14:24 run drwxr-xr-x 2 root root 4096 Apr 10 14:24 sbin drwxr-xr-x 2 root root 4096 Apr 10 14:23 srv dr-xr-xr-x 13 root root 0 Apr 10 14:26 sys drwxrwxrwt 8 root root 4096 Apr 10 14:31 tmp drwxr-xr-x 12 root root 4096 Apr 10 14:24 usr drwxr-xr-x 14 root root 4096 Apr 10 14:32 var
3. Linux File System Types
Linux supports multiple file system types, each with its own characteristics and applicable scenarios. Here are the commonly used file system types:
3.1 Local File Systems
3.1.1 ext4
ext4 is the default file system for Linux, an upgraded version of ext3. It supports larger files and partitions, providing better performance and reliability.
- Maximum file size: 16TB
- Maximum partition size: 1EB
- Features: Journaling file system, supports EXTENTS, delayed allocation, online defragmentation, etc.
3.1.2 XFS
XFS is a high-performance journaling file system developed by SGI, suitable for handling large files and mass store.
- Maximum file size: 8EB
- Maximum partition size: 8EB
- Features: High performance, supports online expansion, suitable for large servers
3.1.3 Btrfs
Btrfs (B-tree file system) is a modern file system that supports advanced features such as snapshots, cloning, and checksums.
- Maximum file size: 16EB
- Maximum partition size: 16EB
- Features: supports snapshots, cloning, checksums, RAID functionality, etc.
3.1.4 ZFS
ZFS is an advanced file system that supports data integrity verification, snapshots, cloning, compression, and other features.
- Maximum file size: 16EB
- Maximum partition size: 256ZB
- Features: Data integrity verification, supports RAID-Z, advanced snapshot functionality
3.2 Network File Systems
3.2.1 NFS
Network File System (NFS) allows different computers to share files over a network.
3.2.2 Samba
Samba allows Linux systems to share files and printers with Windows systems.
3.2.3 SSHFS
SSHFS mounts remote file systems via the SSH protocol, providing secure file sharing.
4. File System Mount managementment
Mounting is the process of connecting a file system to the Linux directory tree. In Linux, store devices need to be mounted to a mount point in the directory tree to access their files.
4.1 Mount Commands
4.1.1 mount Command
The mount command is used to mount file systems:
# Basic mount syntax mount [options] device mount_point # Mount USB device mount /dev/sdb1 /mnt/usb # Mount ISO file mount -o loop /path/to/image.iso /mnt/iso # View mounted file systems mount # View specific file system type mounts mount -t ext4
4.1.2 umount Command
The umount command is used to unmount file systems:
# Unmount by mount point umount /mnt/usb # Unmount by device umount /dev/sdb1 # Force unmount umount -f /mnt/usb # Lazy unmount (delayed unmount) umount -l /mnt/usb
4.2 /etc/fstab File
The /etc/fstab file defines file systems that are automatically mounted at system startup. Each line contains the following fields:
# Device Mount Point File System Type Mount Options Dump Pass /dev/sda1 / ext4 defaults 0 1 /dev/sda2 /home ext4 defaults 0 2 /dev/sda3 swap swap defaults 0 0 /dev/sdb1 /mnt/data ext4 defaults 0 2
4.3 Auto Mounting
The autofs service can automatically mount and unmount file systems as needed, reducing system resource using:
# Install autofs sudo apt install autofs # configuration auto mounting # Edit /etc/auto.master file # Add line: /mnt/autofs /etc/auto.misc # Edit /etc/auto.misc file # Add line: cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom # Restart autofs service sudo systemctl restart autofs
5. File Operation Commands
Linux provides rich commands for file and directory operations:
5.1 Basic file operations
5.1.1 Creating and Deleting Files
# Create empty file touch file.txt # Create and write content echo "Hello World" > file.txt # Append content echo "Additional content" >> file.txt # Delete file rm file.txt # Force delete rm -f file.txt # Confirm before delete rm -i file.txt
5.1.2 Copying and Moving Files
# Copy file cp source.txt destination.txt # Copy directory cp -r source_dir destination_dir # Move file mv source.txt destination.txt # Rename file mv oldname.txt newname.txt
5.1.3 Viewing File Content
# View file content cat file.txt # View with pagination less file.txt # View first n lines head -n 10 file.txt # View last n lines tail -n 10 file.txt # View in real-time tail -f file.txt
5.2 Directory Operations
# Create directory mkdir directory # Create nested directories mkdir -p dir1/dir2/dir3 # Delete empty directory rmdir directory # Delete non-empty directory rm -r directory # Change directory cd directory # Return to parent directory cd .. # Return to home directory cd ~ # Show current directory pwd
5.3 File Search
# Search by name find /path -name "filename" # Search by type find /path -type f -name "*.txt" # Search by size find /path -size +10M # Search by modification time find /path -mtime -7 # Search for files containing specific content grep -r "content" /path
6. File Permissions and Attributes
Linux uses a file permission system to control access to files and directories. Each file and directory has three sets of permissions: owner, group, and other users.
6.1 File Permission Representation
File permissions can be represented using symbols or numbers:
# Symbolic representation -rw-r--r-- 1 user group 0 Apr 10 14:30 file.txt # Numeric representation # r=4, w=2, x=1 # 755 = rwxr-xr-x # 644 = rw-r--r--
6.2 Modifying File Permissions
# Modify permissions using symbols chmod u+x file.txt # Add execute permission for owner chmod g+w file.txt # Add write permission for group chmod o-r file.txt # Remove read permission for others chmod a+r file.txt # Add read permission for all users # Modify permissions using numbers chmod 755 file.txt # Set to rwxr-xr-x chmod 644 file.txt # Set to rw-r--r--
6.3 Modifying File Owner
# Modify owner chown user file.txt # Modify both owner and group chown user:group file.txt # Recursively modify directory and its contents chown -R user:group directory
6.4 File Special Attributes
# Set SUID (Set User ID) chmod u+s file # Set SGID (Set Group ID) chmod g+s file # Set Sticky Bit chmod +t directory # View extended attributes lsattr file # Set extended attributes chattr +i file # Immutable chattr +a file # Append only
7. Linked Files
Linux supports two types of links: hard links and soft links (symbolic links).
7.1 Hard Links
A hard link is another reference to a file's inode, sharing the same inode and data with the original file.
# Create hard link ln file.txt hardlink.txt # View inode ls -i file.txt hardlink.txt
7.2 Soft Links
A soft link is a special file that points to the path of another file or directory.
# Create soft link ln -s file.txt softlink.txt # View soft link ls -l softlink.txt # Link directory ln -s directory link_directory
7.3 Differences Between Hard Links and Soft Links
- Hard Links: Share inode, deleting the original file does not affect hard links, cannot link directories, cannot cross file systems.
- Soft Links: Have their own inode, deleting the original file makes soft links invalid, can link directories, can cross file systems.
8. Practice Case: Managing File Systems
8.1 Case Objective
Create partitions, format file systems, mount and configure automatic mounting.
8.2 Implementation Steps
8.2.1 View Disk Information
# View disk information fdisk -l # View partition information df -h
8.2.2 Create Partition
# Enter fdisk tool fdisk /dev/sdb # Commands: n # Create new partition p # Primary partition 1 # Partition number # Press Enter to use default start sector +10G # Partition size w # Write and exit
8.2.3 Format Partition
# Format as ext4 file system mkfs.ext4 /dev/sdb1
8.2.4 Create Mount Point
mkdir /mnt/data
8.2.5 Mount Partition
mount /dev/sdb1 /mnt/data
8.2.6 configuration Automatic Mounting
# View UUID blkid /dev/sdb1 # Edit /etc/fstab # Add the following line UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /mnt/data ext4 defaults 0 2
8.2.7 Test Automatic Mounting
# Unmount partition umount /mnt/data # Remount all partitions mount -a # View mount status df -h
9. Interactive Exercises
Exercise 1: File System Navigation
Perform the following operations:
- 1. Switch to the root directory and view all directories under the root directory.
- 2. Enter the /etc directory and view the configuration files there.
- 3. Enter the /var/log directory and view the system log files.
- 4. Return to the user's home directory and create a directory named test.
- 5. Create a file named file.txt in the test directory and write "Hello Linux" into it.
Exercise 2: File Permission managementment
Perform the following operations:
- 1. Create a file named secret.txt and write "This is a secret" into it.
- 2. Set the file permissions so that only the owner can read and write.
- 3. Create a directory named share and set permissions so that all users can enter and read, but only the owner can write.
- 4. Create a file in the share directory and test access permissions for different users.
Exercise 3: Linked Files
Perform the following operations:
- 1. Create a file named original.txt and write "Original content" into it.
- 2. Create a hard link and a soft link for original.txt.
- 3. Modify the content of original.txt and view the content changes in the hard link and soft link.
- 4. Delete original.txt and view the status of the hard link and soft link.