Linux fsck Command: Check and Fix Filesystem Errors
The fsck command in Linux is actually a wrapper that serves to call other commands which are variations of fsck for various types of file systems.
You can notice this with the ls command in the /usr/sbin directory:
$ ls -l /usr/sbin/*fsck*lrwxrwxrwx 1 root root 8 mar 8 2019 /usr/sbin/dosfsck -> fsck.fat-rwxr-xr-x 4 root root 256424 jul 26 2018 /usr/sbin/e2fsck-rwxr-xr-x 1 root root 49896 jul 26 2018 /usr/sbin/fsck-rwxr-xr-x 1 root root 37200 jul 26 2018 /usr/sbin/fsck.cramfs-rwxr-xr-x 4 root root 256424 jul 26 2018 /usr/sbin/fsck.ext2-rwxr-xr-x 4 root root 256424 jul 26 2018 /usr/sbin/fsck.ext3-rwxr-xr-x 4 root root 256424 jul 26 2018 /usr/sbin/fsck.ext4-rwxr-xr-x 1 root root 57424 ago 2 2018 /usr/sbin/fsck.fat-rwxr-xr-x 1 root root 83536 jul 26 2018 /usr/sbin/fsck.minixlrwxrwxrwx 1 root root 8 mar 8 2019 /usr/sbin/fsck.msdos -> fsck.fatlrwxrwxrwx 1 root root 8 mar 8 2019 /usr/sbin/fsck.vfat -> fsck.fat-rwxr-xr-x 1 root root 433 jan 24 2019 /usr/sbin/fsck.xfs
Depending on the file system, fsck invokes the appropriate fsck.something to handle that file system.
The purpose of fsck is to check and correct the existence of errors in the file system. By default, fsck assumes the ext2 file system and, after performing a check on the disk, asks the user if they want to make the necessary corrections.
As a parameter, fsck accepts a device name (e.g.: /dev/hdc1, /dev/sdb2), a mount point (e.g: /, /usr, /home), or a Label (e.g.: LABEL=root) or a UUID disk identifier (e.g.: UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd ).
A UUID identifier is a unique number that identifies a disk, like the "MAC Address of disks".
The frequently used options are:
- -A: Performs the check of all disks specified in the /etc/fstab file;
- -t: Specifies the type of file system that should be checked;
We can also have some options for the ext2 file system:
- -b: Superblock Specifies which superblock fsck will work with;
- -c: Performs check for bad sectors (bad blocks);
- -f: Forces the file system check even if it appears clean;
- -p: Automatically repairs the file system;
- -y: Runs fsck in non-interactive mode, not asking any questions to the user;
A superblock is a special area on the disk with important information about the file system parameters and its current state. Usually the superblock is copied to several areas on the disk as backup.
Fsck requires the disk to be unmounted to perform the check and repairs:
# fsck /dev/xvdb1/dev/xvdb1 is in use.
Once the file system is unmounted:
# fsck.ext4 /dev/xvdb1e2fsck 1.42.9 (28-Dec-2013)/dev/xvdb1: clean, 41436/524288 files, 374905/2096891 blocks
Depending on the size and the numerous repairs the partition needs, the fsck process can take hours.
To check the root system partition there are two options:
- Use a Pendrive with Linux to boot another Linux and check the partition;
- Use the "touch /forcefsck" command to create the forcefsck file, which forces Linux to check the root partition at system boot
Command e2fsck
The e2fsck command is used to check the ext2, ext3 and ext4 file systems. It is the version of fsck for file systems based on the ext file system.
The options are common to fsck, since it is invoked by fsck when dealing with ext2, ext3 and ext4 type partitions.
Example:
$ sudo fsck.ext4 /dev/xvdb1e2fsck 1.42.9 (28-Dec-2013)/dev/xvdb1: clean, 41436/524288 files, 374905/2096891 blocks
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.