fsck Command Cheat Sheet
fsck (FileSystem Consistency ChecK) acts as a frontend for various filesystem checkers (fsck.ext4, fsck.xfs, fsck.vfat, etc.). It allows you to check and repair Linux filesystems.
⚠️ WARNING: Never run fsck on a mounted partition (except inside a Read-Only mount). Doing so can severely corrupt data.
Synopsis
fsck [OPTION]... [FILESYSTEM]...
Basic Usage
Check a Partition
Unmount first!
sudo umount /dev/sdb1
sudo fsck /dev/sdb1
Auto-Repair (-y)
Answer "yes" to all repair questions automatically (non-interactive).
sudo fsck -y /dev/sdb1
Dry Run (-n)
Don't make changes. Just check and report what would be done.
sudo fsck -n /dev/sdb1
Root Filesystem Check
Since you cannot unmount the root partition (/) while the OS is running, you have two options:
1. Force Check on Next Boot
Create a file named forcefsck in the root directory.
sudo touch /forcefsck
sudo reboot
fsck, and remove the file.
2. Boot into Recovery Mode
Boot from a Live USB or select "Recovery Mode" in GRUB to run fsck on the unmounted root.
Common Options
Force Check (-f)
By default, fsck skips filesystems marked as "clean". Use -f to force a scan anyway.
sudo fsck -f /dev/sdb1
Scanning All Filesystems (-A)
Usually used in system boot scripts. Checks all FS listed in /etc/fstab.
sudo fsck -A
-R: Skip the root filesystem (useful if running script on live system).-M: Do not check mounted filesystems.
Filesystem Specifics
fsck detects the type and calls the appropriate backend.
Ext4 / Ext3 / Ext2
Uses e2fsck.
- Supports bad block check (-c).
- Supports optimizing directories (-D).
XFS
XFS is designed to essentially rarely need fsck. fsck.xfs does nothing! If meaningful repair is needed, use xfs_repair (requires unmount).
sudo xfs_repair /dev/sdb1
FAT / VFAT
Uses fsck.vfat (or dosfsck).
Superblock Recovery
If a filesystem's superblock is corrupted, it won't mount.
-
Find backup superblocks:
(Note:sudo mke2fs -n /dev/sdb1-nacts as dry-run make, listing where superblocks WOULD be). -
Restore from backup:
(Replace 32768 with a block number found in step 1).sudo e2fsck -b 32768 /dev/sdb1
Exit Codes
The exit code of fsck is a bitmask of the following values:
| Bit | Value | Meaning |
|---|---|---|
| 0 | 1 | Filesystem errors corrected |
| 1 | 2 | System should be rebooted |
| 2 | 4 | Filesystem errors left uncorrected |
| 3 | 8 | Operational error |
| 4 | 16 | Usage or syntax error |
| 5 | 32 | Checkout canceled by user |
| 7 | 128 | Shared library error |
Example: Exit code 3 (1 + 2) means errors were corrected AND you should reboot.
Notes
- Lost+Found: Recovered file fragments (orphaned inodes) are placed in the
lost+founddirectory of the partition. They are named by their inode number (e.g.,#12345). Usefilecommand to identify them. - Dirty Bit: If a system crashes, the "dirty bit" is set on the FS.
fsckruns automatically on boot to clean it.