The FSCK command is a very important Linux/Unix utility, it is used to check & repair the errors in the file system. It is similar to 'chkdsk' utility in Windows operating systems. It is available for Linux, macOS, FreeBSD operating systems.

FSCK stands for File System Consistency Check & most of the time, it runs at boot time but can also be started manually by the superuser, if the need arises.

It can be used with 3 modes of operation,

1- Check for errors & let the user decide what should be done with each error,

2- Check for errors & make repairs automatically, or,

3- Check for errors & display the error but does not perform any repairs.

(Recommended Read: Learn to use TCPDUMP command with examples)


The syntax for using FSCK

We can use the FSCK command manually with the following syntax,

$ fsck options drives

The options that can be used with the fsck command are,

-p      Automatic repair (no questions)

-n      Make no changes to the filesystem

-y      Assume "yes" to all questions

-c      Check for bad blocks and add them to the bad block list

-f      Force checking even if the filesystem is marked clean

-v     Be verbose

-b     superblock Use alternative superblock

-B     blocksize Force blocksize when looking for the superblock

-j      external_journal Set location of the external journal

-l      bad_blocks_file Add to bad blocks list

-L     bad_blocks_file Set bad blocks list

We can use any of the following options, depending on the operation we need to perform. Let's discuss some of the options of the fsck command with examples.


Fsck command with examples

Note:- Before we discuss any examples, please read this. We should not be using FSCK on mounted drives, as there will be high chances that fsck on the mounted drive will damage the drive permanently. So before performing fsck, we must unmount the drive with the following command,

$ umount drive-name

For example,

$ umount /dev/sdb1

You can check the partition number with the following command,

$ fdisk -l

Also while running the fsck, we might get some error codes. Below mentioned is the list of error codes that we might get along with their meanings,

0 - No errors
1 - Filesystem errors corrected
2 - The system should be rebooted
4 - Filesystem errors left uncorrected
8 - Operational error
16 - Usage or syntax error
32 - Fsck canceled by user request
128 - Shared-library error

Now let's discuss usage of the fsck command with examples,

 

Perform an error check on a single partition

To perform a file check on a single partition, run the following command from the terminal,

$ umount /dev/sdb1

$ fsck /dev/sdb1

 

Check filesystem for errors & repair them automatically

Run the fsck command with 'a' option to perform consistency check & to repair them automatically, execute the following command. We can also use the 'y' option in place of option 'a'.

$ fsck -a /dev/sdb1

 

Check filesystem for errors but don't repair them

In case we only need to see the error that is occurring on our file system & doesn't need to repair them, then we should run fsck with option 'n',

$ fsck -n /dev/sdb1

 

Perform an error check on all partitions

To perform a filesystem check for all partitions in single go, use fsck with 'A' option,

$ fsck -A

To disable the root file system check, we will use the option 'R'

$ fsck -AR

 

Check the only partition with mentioned filesystem

In order to run fsck on all the partitions with mentioned filesystem type, for example, 'ext4', use fsck with option 't' followed by filesystem type,

$ fsck -t ext4 /dev/sdb1

or

$ fsck -t -A ext4

 

Perform consistency check only on unmounted drives

To make sure that the fsck is performed only on unmounted drives, we will use the option 'M' while running fsck,

$ fsck -AM

Get the complete list of options with the help

$ fsck --help

fsck command

This was our tutorial on the fsck command with examples. Please feel free to send in your questions or queries to us, using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.