The File System Checker (fsck) is a system utility in Linux/Unix used to check and repair inconsistencies in the file system. It scans file systems to detect and correct issues related to file system integrity, such as corrupted files, bad blocks, or incorrect metadata, ensuring that the file system remains consistent and usable.
Basic fsck
Commands:
Check and Repair a Specific Partition:
To check a specific partition and attempt repairs, use:
sudo fsck /dev/sda1
This will run fsck
on the partition /dev/sda1
.
Running fsck
on All File Systems:
To check and repair all file systems that are listed in the /etc/fstab
file, use the -A
option:
sudo fsck -A
Forcing a Check:
Sometimes, fsck
will skip checks on a “clean” file system. You can force a check using the -f
flag:
sudo fsck -f /dev/sda1
Interactive Mode: fsck
can be run in interactive mode, asking for confirmation before making repairs:
sudo fsck -r /dev/sda1
Automatic Repairs: Use the -y
option to automatically fix any issues found (use with caution):
sudo fsck -y /dev/sda1
Skip Certain Filesystems: You can skip certain file systems like network file systems with the -C
or -T
options to exclude certain types from checks.
Key Concepts of fsck
:
When to Use fsck
:
- The system crashes or shuts down unexpectedly (e.g., due to a power failure).
- Files or directories appear to be missing or corrupted.
- The system won’t boot, and a file system inconsistency is suspected.
- A specific partition is behaving unusually (slow performance, errors).
How fsck
Works: The fsck
tool works by checking the metadata of the file system (e.g., inodes, superblocks, data blocks) and detecting issues such as:
- Incorrect file size in metadata.
- Bad sectors or blocks on the disk.
- Corrupted superblock or inode entries.
- Lost clusters or orphaned files that are no longer attached to any directory.
File Systems Supported by fsck
: fsck
supports a variety of file systems, such as:
- ext2, ext3, ext4 (common Linux file systems).
- XFS, JFS, ReiserFS (other file systems used in Linux).
- VFAT, NTFS (for checking Windows partitions).
For each supported file system, fsck
invokes the appropriate file system-specific checker, such as:
e2fsck
: For ext2/ext3/ext4 file systems.fsck.xfs
: For XFS file systems.fsck.vfat
: For FAT partitions.