Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
sudo fsck /dev/sdb1          # read-only example: check, ask before each repair
sudo fsck -n /dev/sdb1       # only report, change nothing

Try it

Bash Decoding the exit status

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.

Bash Which entries are checked at boot?

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

Terminal
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. umount the 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 (with dd to another disk if the device is failing) before running repairs.
  • Do not use -y blindly on a disk with hardware problems. Repairs on a failing drive can destroy more data. Check dmesg for I/O errors first (dmesg).
  • Clean means skipped. e2fsck skips 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 in lost+found under their inode numbers (#12345). Look there after a repair. See Inodes.
  • XFS uses xfs_repair (device unmounted) and xfs_repair -n for dry runs; Btrfs uses btrfs check and btrfs scrub; fsck.xfs does nothing by design.
  • Boot-time checks. The pass field in /etc/fstab decides 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. fsck restores consistency, not content: files that were damaged stay damaged.