Safely Check a Linux Filesystem with fsck

Run fsck on the wrong device and you can hand a checker permission to rewrite metadata it should never have touched. This guide identifies the filesystem checker that fsck would run, performs a controlled check on an unmounted filesystem, and interprets the result without treating every non-zero status as a catastrophe. Allow 15 minutes for a simple check and longer for a large or busy disk. You need a shell, the util-linux fsck front-end, and normally elevated privileges for the actual check.

Warning: A filesystem repair can change or remove metadata. Never start a repair on a mounted filesystem, and keep a current backup before you approve corrections. The examples below begin with a dry run and use placeholders deliberately. Replace them only after confirming the device identity.

1. Check the local version and the target

fsck is a front-end: it selects a filesystem-specific program such as fsck.ext4 rather than implementing every filesystem format itself. The installed command on this machine reports util-linux 2.41.3; the local manpage was generated from util-linux 2.39.3. Option details below are tied to the local manual and should be rechecked after a major upgrade.

$ command -v fsck
/home/linuxbrew/.linuxbrew/sbin/fsck
$ fsck --version
fsck from util-linux 2.41.3

Find the candidate device and its current mount state before doing anything that can write metadata. Use a real device path from your own host, not the placeholder in the example:

$ lsblk -f
$ findmnt --source /dev/DEVICE_PARTITION

Checkpoint: if findmnt prints a mount point, stop. Unmount it during an appropriate maintenance window, or use a recovery environment where the target is not mounted. Do not unmount a live root or service filesystem casually.

2. Preview the checker command

-N means do not execute the checkers, while -V shows the filesystem-specific commands that would be executed. Together they are a useful checkpoint for catching a wrong device or an unexpected filesystem type. This example is non-destructive and does not require sudo:

$ fsck -N -V /dev/DEVICE_PARTITION
fsck from util-linux 2.41.3
[/usr/sbin/fsck.ext2 (1) -- /dev/does-not-exist] fsck.ext2 /dev/does-not-exist

The output above is illustrative of the installed command's format. Your line should name the device you supplied and the checker appropriate to its filesystem: an ext4 device may be handled by fsck.ext4 or the compatible e2fsck family. If the preview names the wrong device or checker, fix the target and repeat this step.

3. Run a check on an unmounted filesystem

Once the preview is correct and the device is unmounted, run the check as root. The basic command lets the filesystem-specific checker use its normal policy:

$ sudo fsck /dev/DEVICE_PARTITION

Expect checker-specific output. Some checkers ask questions before correcting errors; others have different defaults. That behaviour is not standardised by fsck. Read the manual for the selected checker before adding options, especially ones that approve repairs automatically.

For several filesystems, fsck may work on them in parallel when their /etc/fstab pass numbers and physical devices permit it. Use -s for serial operation, particularly when interactive checkers would otherwise compete for your attention:

$ sudo fsck -s /dev/DEVICE_PARTITION

There is no general undo command for repaired metadata. If the check reports corrections, preserve its output and compare the filesystem with your backup. Do not delete the original device, recreate its partition or rerun a repair option merely to silence a warning.

4. Use -M when a mounted target must be skipped

-M tells fsck not to check mounted filesystems and returns status 0 for those skipped targets. That is a guardrail, not permission to ignore the mount state:

$ sudo fsck -M /dev/DEVICE_PARTITION
$ printf 'fsck status: %s\n' "$?"
fsck status: 0

A zero here can mean that no errors were found, or that the mounted filesystem was skipped. Check with findmnt as well. If you need to check the root filesystem, plan a boot-time or recovery-environment check rather than forcing a live check.

5. Check filesystems listed in /etc/fstab

With no filesystem argument, fsck checks the filesystems selected by its normal /etc/fstab behaviour. -A explicitly walks that file. Entries with a pass number of zero are skipped; pass number 1 is normally used for the root filesystem and pass number 2 for other filesystems. Inspect the file before relying on a bulk check:

$ awk '$1 !~ /^#/ && NF >= 6 { print $1, $2, $3, "pass=" $6 }' /etc/fstab
$ fsck -N -A -V

Use -R with -A to skip the root filesystem, for example when it is already mounted read-write and you are checking other entries from a maintenance environment:

$ sudo fsck -N -A -R -V

Keep the dry-run flag until the proposed list is exactly what you intend. Non-existent devices can still lead to boot-time repair problems; the manual recommends the nofail mount option for entries that are allowed to be absent.

6. Read the exit status instead of guessing

Capture the status immediately after the command. The status is a bit mask, and multiple filesystem results are combined with bitwise OR:

$ sudo fsck /dev/DEVICE_PARTITION
$ status=$?
$ printf 'fsck status: %s\n' "$status"
fsck status: 0

For automation, test bits rather than matching one exact number. Status 5, for example, means a correction occurred and some error condition remains:

$ if (( status & 4 )); then
>     printf '%s\n' 'Uncorrected filesystem errors remain.'
> fi
$ if (( status & 2 )); then
>     printf '%s\n' 'A reboot is recommended.'
> fi

Do not pass options intended for one checker through a complicated fsck command and assume another checker understands them. Options after -- are forwarded, but filesystem-specific options are not standardised. For advanced work, call the named checker directly after reading its manual.

Done means