fsck: Repair File System Errors in Linux
In this article
Yes, fsck (file system check) is the standard Linux tool for detecting and repairing file system errors. It checks the consistency of a file system and can fix common problems like corrupted inodes, bad blocks, and incorrect directory entries, provided the file system is unmounted or read-only.
What Does fsck Actually Do?
fsck is a front-end that calls file-system-specific checkers, such as e2fsck for ext2/ext3/ext4, fsck.xfs for XFS, and fsck.btrfs for Btrfs. It scans the file system's metadata—the data structures that track files, directories, permissions, and free space—and verifies that they are internally consistent. It looks for issues like:
- Inodes that reference blocks already marked as free
- Files with incorrect link counts
- Directory entries pointing to nonexistent inodes
- Superblock inconsistencies (e.g., wrong free-block count)
- Corrupted extent maps or block bitmaps
When it finds a problem, it either fixes it automatically (if you pass the -y flag) or prompts you for each fix. The goal is to make the file system usable again, not to recover deleted files—though sometimes fixing a directory can make orphaned files accessible again.
When Should You Run fsck?
Linux automatically runs fsck at boot if the file system was not unmounted cleanly (e.g., after a power failure). You can also run it manually when you see symptoms like:
- Files that disappear or become unreadable
- Errors like "Input/output error" when accessing files
- System fails to boot with a message about checking the disk
- Unusual directory entries (e.g., files with names like
#12345)
You should never run fsck on a mounted file system, except for the root file system in a special read-only mode. Running it on a mounted, writable file system can cause further corruption because the kernel is still writing to the disk while fsck is trying to fix it. For the root partition, you either boot from a live USB, use a recovery shell, or run fsck -n in read-only mode if you must check it while mounted (though this is risky).
How to Run fsck Safely
Follow these steps to check and repair a non-root partition:
- Unmount the partition:
sudo umount /dev/sdXY(replacesdXYwith your device, e.g.,sda1). - Run the check:
sudo fsck -f /dev/sdXY. The-fforces a full check even if the file system appears clean. - Review the output. fsck will list each error and ask "Fix?"—type
yfor yes, or pressnto skip. - If you want to auto-fix everything without prompts, use
sudo fsck -y /dev/sdXY. - After it finishes, remount the partition:
sudo mount /dev/sdXY.
For the root partition, you can boot into recovery mode (hold Shift at GRUB, select "Advanced options" then "Recovery mode"), choose "fsck" from the menu, and it will remount the root as read-only and run fsck. Alternatively, boot from a live USB and run fsck on your root partition while it is unmounted.
Common fsck Options and Their Meaning
-f— Force a check even if the file system was cleanly unmounted.-y— Assume "yes" to all prompts; useful for unattended repairs.-n— Assume "no" to all prompts; performs a read-only check without fixing anything.-p— Preen mode: automatically fixes safe issues without asking, used at boot.-t fstype— Specify the file system type (e.g.,fsck -t ext4 /dev/sdb1).
You can also check the file system type with blkid /dev/sdXY or lsblk -f. If you are unsure, running fsck without -t will try to detect it automatically.
What Happens When fsck Finds a Problem?
When fsck finds a serious error, it may place affected files in a lost+found directory at the root of the file system. These are files whose names or directory entries were too corrupted to restore. You can review them with ls -la /lost+found and try to identify them by their content (e.g., file /lost+found/#12345). For minor issues, fsck simply repairs the metadata in place—for example, updating the free-block count or clearing an invalid inode flag.
If fsck reports "superblock is corrupted" or "bad magic number in super-block," you can use a backup superblock. For ext4, run sudo fsck -b 32768 /dev/sdXY (the backup superblock location is often 32768, but you can find it with mke2fs -n /dev/sdXY). This is a more advanced repair that should be attempted only if you have backed up your data.
fsck vs. Other Repair Tools
fsck is the classic tool, but it is not the only one. For XFS, the command is xfs_repair, which is similar but has its own syntax and must run on an unmounted file system. For Btrfs, you use btrfs check (or btrfsck), which can be more complex because Btrfs has built-in checksumming and self-healing features. For ZFS, you typically use zpool scrub to verify data integrity rather than a repair tool, because ZFS handles errors automatically. If you are using LVM, you may need to activate the volume group first (vgchange -ay) before running fsck on a logical volume.
For bad blocks (physical damage), fsck can mark them as bad, but it cannot repair the disk. Tools like badblocks can scan for them, and smartctl can check the drive's health. If fsck repeatedly finds new errors, the disk may be failing—back up your data immediately.
Safety and Data Loss Risks
Running fsck is generally safe, but it is not without risk. If you run it on a mounted file system, you can cause irreparable damage. Even on an unmounted file system, fsck may delete corrupted files or truncate files to a valid size, which can result in data loss. Always back up important data before running a repair. Also, be cautious with -y: it will automatically apply fixes that could be wrong if the file system is severely corrupted. In such cases, it is better to run fsck interactively and review each fix, or seek professional data recovery help.
Another misconception is that fsck can fix all boot problems. It only repairs file system metadata; if the boot issue is caused by a failing hard drive, bad RAM, or a corrupted bootloader, fsck will not help. Check dmesg for hardware errors and run memtest86 if you suspect RAM.
Frequently Asked Questions
Can I run fsck on a mounted partition?
No, you should never run fsck on a mounted, writable file system. It can cause severe corruption because the kernel is actively modifying the file system while fsck is trying to repair it. The only exception is a read-only check with fsck -n, but even that is not recommended. Always unmount the partition first, or boot from a live USB for the root partition.
What does "fsck returned an error code" mean?
fsck returns exit codes that indicate the result: 0 means no errors, 1 means errors were corrected, 2 means the system should be rebooted, 4 means errors were left uncorrected, and 8 means an operational error (e.g., the device is not found). If you see a non-zero code, check the output for details and rerun with -y if needed.
How long does fsck take?
It depends on the size of the partition and the number of errors. A clean 1 TB drive can take several minutes; a drive with many errors can take hours. You can monitor progress with -C (for ext4) to show a progress bar. If it seems stuck, wait—fsck is often slower on large drives.
Will fsck recover deleted files?
No, fsck does not recover deleted files. It only repairs file system metadata. If you accidentally deleted a file, you need a dedicated recovery tool like extundelete or photorec. However, if a file was orphaned due to corruption (not deletion), fsck might place it in lost+found.
Why does fsck run at every boot?
If fsck runs every boot, it usually means the file system was not cleanly unmounted (e.g., after a crash) or the mount count has reached the maximum (for ext4, the default is 30 mounts or 180 days). You can check with tune2fs -l /dev/sdXY and reset the counter with tune2fs -C 0 /dev/sdXY, but only if the file system is clean.
Is fsck safe on an SSD?
Yes, fsck is safe on SSDs, but it will not find bad blocks in the same way as on HDDs because SSDs handle bad blocks internally. The file system check itself is identical. However, frequent fsck runs are unnecessary on a healthy SSD and can cause unnecessary writes, so only run it when you suspect a problem or after an unclean shutdown.