fsck Command in Linux



The fsck command in Linux checks and repairs the filesystem. This tool is especially used to fix problems arising from file corruption. It also assists in data recovery and system maintenance. During boot, the system automatically runs this command to check for errors and to ensure a secure boot. Regularly running this command reduces the risk of random system crashes.

Table of Contents

Here is a comprehensive guide to the options available with the fsck command −

Syntax of fsck Command

The syntax of the Linux fsck command is as follows −

fsck [options] [filesystem]

The [options] field is used to specify the options to change the command's behavior. The [filesystem] field is used to specify the filesystem that needs to be checked and repaired. The [filesystem] can be a device name (e.g. /dev/sda), mount point (e.g. /mnt), ext2 label, or a UUID.

Options of fsck Command

The options for the fsck command are listed below −

Options Description
-A To check all filesystems
-a It automatically repairs the filesystem
-C ["fd"] It displays the progress bar (The file descriptor will be displayed if executed on the GUI front end)
-l It locks the device to guarantee exclusive access
-M It skips the check on mounted devices and returns a 0 exit code
-N It dry-runs the command (does not make changes)
-n It reports the filesystem issues but does not repair them
-P It checks filesystems in parallel
-R It skips checking the root filesystem when using -A
-r ["fd"] It reports statistics of each filesystem checked (The file descriptor will be displayed if executed on the GUI front-end)
-s It serializes the filesystem checks
-T To skip displaying the title on the start
-t type It is used to specify the filesystem type to be checked
-V It displays the verbose output
-h / -? It displays a brief help related to the command
--version It displays the command version
-y In some filesystem-specific fsck tools automatically fixes detected corruption without user confirmation

Exit Codes of fsck Command

The exit codes that are returned by the fsck command are listed below −

Exit Codes Description
0 It indicates no errors
1 It indicates the filesystem error detected
2 It indicates the system should not be rebooted
4 It indicates the filesystem errors left uncorrected
8 It indicates the specific operational error
16 It indicates the syntax error
32 It indicates the command execution was canceled by the user
128 It indicates the shared library error

Examples of fsck Command in Linux

This section demonstrates the usage of the fsck command in Linux with examples.

Before checking the filesystem of a device, it must be unmounted. To unmount a filesystem, use the umount command −

sudo umount /dev/sda1

Note − A root filesystem cannot be unmounted.

After checking the filesystem, you may need to mount it back. To mount back the filesystem, use the mount command −

sudo mount /dev/sda1

Dry Running

To view the standard output of the fsck execution without actually making any checks and repairs, use the -N option −

sudo fsck -N /dev/sda1

The fsck command required sudo permission.

Checking Filesystem

To check for errors, use the fsck command with the filesystem name.

sudo fsck /dev/sda1
fsck Command in Linux1

The output will walk you through the various prompts such as whether you want to back up the filesystem or not, remove the dirty bit, and confirm the changes.

fsck Command in Linux2

Other prompts that the fsck command can ask are shown in the image below −

fsck Command in Linux3

To quit the filesystem check, press q.

Checking Filesystems in Serial Mode

It is good practice to check multiple filesystems using serial mode. Serial mode allows to sequentially check the filesystem. The -s option is used for serial mode.

sudo fsck -s /dev/sda1 /dev/sda2

The above command will process the first filesystem /dev/sda1 and then /dev/sda2. This reduces the complexities that could arise during decision-making on prompts.

Skipping Repair

To view the errors without rectifying them, use the fsck command with the -n option −

sudo fsck -n /dev/sda1
fsck Command in Linux4

Skipping Prompting

By default, the fsck command prompt for every change it tries to make. To skip these prompts, use the -y option −

sudo fsck -y /dev/sda1

It should be noted that -y forces the fsck to automatically answer "yes" to all prompts.

Performing Checks on All Filesystems

To perform checks on all filesystems, use the -A option.

sudo fsck -A

To skip to the root filesystem, use the -Rb

sudo fsck -AR
fsck Command in Linux5

While checking the entire filesystem the skipped mounted filesystem will be displayed in the output.

Forcing a Filesystem Check

To perform a force check on a clean filesystem, use the -f option.

sudo fsck -f /dev/sda

By default, fsck checks only filesystems marked for review. The -f option forces a check regardless of the filesystem's state.

Skipping a Specific Filesystem

To skip a specific filesystem while performing the check, use the -t option with the filesystem type −

sudo fsck -AR -t !ext2

The above command will skip the ext2 type filesystem. In the place of !, the no keyword can also be used (e.g. noext2).

Note that one or more filesystem types can be specified by separating them with commas. Simply, use !/no with the filesystem type that needs to be skipped.

sudo fsck -AR -t !ext2,ext3

The above will skip the ext2 and check the ext3 filesystems only.

Skipping a Mounted Filesystem

To skip the mounted filesystem, use the -M option −

sudo fsck -AR -M
fsck Command in Linux6

Checking Filesystem with Progress Bar

To check the filesystem with the progress bar, use the -C option −

sudo fsck -C /dev/sda1

Checking Filesystem in Verbose

It is a good practice to check filesystems in verbose to get detailed output −

sudo fsck -V /dev/sda1

Conclusion

The fsck command in Linux is a handy tool to check and repair the corrupted filesystem. It is a standard Linux tool and does not require additional tools to use it. Even Linux systems auto-execute this command to check filesystem error before booting. It is recommended to regularly check the filesystem to avoid any system crashes due to filesystem corruption.

In this tutorial, we explained the fsck command, its syntax, options, exit codes, and usage in Linux with examples.

Advertisements