fsck
fsck is the Linux tool that checks a disk’s filesystem for damage and fixes it. If a computer loses power suddenly, the filing system on the disk can be left inconsistent. fsck walks through it, finds things that do not add up, and repairs them. It is the Linux cousin of Windows’ chkdsk.
fsck is a front end that calls the right checker for each filesystem: e2fsck (ext2/3/4), fsck.vfat, xfs_repair (XFS uses its own tool), btrfs check.
What it checks (ext4): superblock sanity, inode and block bitmaps, directory structure, link counts, and orphaned inodes. Orphans are placed in lost+found.
Rules:
- Never run fsck on a mounted read-write filesystem. It can cause the corruption it is meant to fix.
- To check root, boot a live USB, use recovery mode, or let it run at boot.
-nchecks without changing anything;-yanswers yes to every fix;-fforces a check even if the filesystem looks clean.
At boot: the sixth fstab field controls order (1 for root, 2 for others, 0 to skip). systemd runs systemd-fsck@.service for each. A failing check drops you to emergency mode or the initramfs prompt.
Journaling means most crashes only require a quick journal replay, not a full fsck. Repeated errors usually point at failing hardware, so check SMART data too.
Source: fsck(8) Linux manual page
Checking and repairing a filesystem
# Unmount first (a data disk)
$ sudo umount /mnt/data
# Read-only check
$ sudo fsck -n /dev/sdb1
e2fsck 1.47.0
data: clean, 11/655360 files, 66753/2621440 blocks
# Force a full check and fix
$ sudo fsck -fy /dev/sdb1
# Root filesystem from the initramfs prompt
(initramfs) fsck -y /dev/sda2
# When was it last checked?
$ sudo tune2fs -l /dev/sda2 | grep -i "last checked" fsck is part of nearly every “it will not boot after a power cut” recovery. The safe pattern: unmount (or boot from rescue media), run fsck -n to see the damage, then fsck -y to fix it, then look at why it happened. If the same disk needs repair again soon, treat it as a hardware warning and back it up. In BNLC Phase 4 you practice this on a VM disk you can snapshot first.