Lesson 50: fsck Command
In this lesson, you'll learn how to use the fsck command to check and repair Linux filesystem errors.
Linux filesystems are responsible for organizing how data is stored and recovered.
One way or another, with time, the filesystem may become corrupted, and certain parts of it may not be accessible.
If your filesystem develops such inconsistency, it is recommended to verify its integrity.
This can be completed via a system utility called fsck (file system consistency check), which checks the root file system automatically during boot time or runs manually.
When to Use fsck
There are different scenarios when you will want to run fsck. Here are a few examples:
- The system fails to boot.
- Files on the system become corrupt (often you may see an input/output error).
- The attached drive (including flash drives/SD cards) is not working as expected.
fsck Command Syntax
# fsck [OPTIONS] DEVICE
fsck Command Options
The fsck command needs to be run with superuser privileges or root. You can use it with different arguments.
Their usage depends on your specific case. Below you will see some of the more important options:
| Option | Description |
|---|---|
-A |
Check all filesystems listed in /etc/fstab |
-C |
Show a progress bar |
-l |
Lock the device to guarantee no other program uses the partition during the check |
-M |
Do not check mounted filesystems |
-N |
Only show what would be done - no actual changes are made |
-P |
Check filesystems in parallel, including root |
-R |
Do not check the root filesystem (useful only with -A) |
-r |
Provide statistics for each device being checked |
-T |
Do not show the title |
-t TYPE |
Exclusively specify the Linux filesystem types to be checked (comma-separated) |
-V |
Provide a description of what is being done |
-y |
Automatically answer "yes" to any prompts to correct errors |
1. Run fsck on a Partition
In order to run fsck, you will need to ensure that the partition you are going to check is not mounted.
For this purpose, let's use a second drive /dev/sdb mounted in /mnt.
Here is what happens if you try to run fsck when the partition is mounted:
# fsck /dev/sdb
fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/sdb is mounted.
e2fsck: Cannot continue, aborting.
To avoid this, unmount the partition using:
# umount /dev/sdb
Then fsck can be safely run with:
# fsck /dev/sdb
fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/sdb: clean, 11/2621440 files, 259044/10485760 blocks