Linux Command Reference ยท Disks and Filesystems
Linux fsck command
Reference for the Linux fsck command: check and repair unmounted filesystems, read the exit code bits, understand the fstab pass number and use -n, -f and -y safely.
fsck ("filesystem check") examines a filesystem for inconsistencies and can repair them. A crash or power loss can leave metadata half-written: a block marked free while a file still uses it, a directory entry pointing nowhere. fsck walks these structures, reports every problem and, if you allow it, fixes them.
Like mkfs, fsck is a front end that chooses the real checker (e2fsck for ext2/3/4, xfs_repair for XFS, fsck.vfat and others). The golden rule: check only unmounted filesystems (or mounted read-only), because a live filesystem changes underneath the checker and repairs can corrupt it. These tools are not installed in our sandbox, so the commands are read-only; the live examples decode its exit status and the boot-time pass numbers.
sudo fsck /dev/sdb1 # read-only example: check, ask before each repair sudo fsck -n /dev/sdb1 # only report, change nothing
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
fsck [-A] [-N] [-P] [-R] [-t TYPE] [-V] [FS-OPTIONS] [DEVICE|MOUNTPOINT|LABEL=|UUID=]...
| Option | Description |
|---|---|
-t TYPE |
Filesystem type; runs fsck.TYPE. Usually detected from the device. |
-A |
Check every filesystem listed in /etc/fstab (what runs at boot), in pass order. |
-R |
With -A, skip the root filesystem. |
-N |
Show what would be done, do nothing. |
-V |
Verbose. |
-n |
(checker option) Answer "no" to all repair questions: report only. |
-y |
(checker option) Answer "yes" to all: repair automatically. |
-p |
(e2fsck) Automatic safe repair without questions, used at boot. |
-f |
(e2fsck) Check even if the filesystem is marked clean. |
-b N |
(e2fsck) Use backup superblock at block N. |
Exit status is the sum of these values:
| Value | Meaning |
|---|---|
| 0 | No errors. |
| 1 | Errors were corrected. |
| 2 | System should be rebooted. |
| 4 | Errors were left uncorrected. |
| 8 | Operational error. |
| 16 | Usage or syntax error. |
| 32 | Canceled by the user. |
| 128 | Shared-library error. |
Usage notes
- Unmounted only.
umountthe filesystem first (umount). For the root filesystem, boot into rescue mode or a live system, or schedule a check at the next boot. - Start with
-n. A report-only run shows how bad things are and cannot make them worse. Back up important data (withddto another disk if the device is failing) before running repairs. - Do not use
-yblindly on a disk with hardware problems. Repairs on a failing drive can destroy more data. Checkdmesgfor I/O errors first (dmesg). - Clean means skipped.
e2fsckskips a filesystem whose journal says it is clean, unless you pass-f. Journaling filesystems usually recover in seconds by replaying the journal. lost+found. Files whose directory entry was lost are reconnected inlost+foundunder their inode numbers (#12345). Look there after a repair. See Inodes.- XFS uses
xfs_repair(device unmounted) andxfs_repair -nfor dry runs; Btrfs usesbtrfs checkandbtrfs scrub;fsck.xfsdoes nothing by design. - Boot-time checks. The pass field in
/etc/fstabdecides the order. Filesystems with the same pass number on different disks may be checked in parallel. - Large disks take time. A full check of many terabytes can run for hours; progress is shown with
-C(e2fsck). - In scripts, test the exit bits with
&, not with==, because values combine (for example 3 = corrected and reboot). - Not a backup tool.
fsckrestores consistency, not content: files that were damaged stay damaged.
Related
mkfs,mount,umount,blkid,/etc/fstab,dmesg- Lessons: Rescue and Recovery, Disks and Logs