Skip to Content

Check Filesystem with Fsck Command in Linux

fsck is a front-end program and it calls the respective program for the specific filesystem to run. Fsck usually runs after the system fails to boot, the file system becomes corrupted, or the attached drive fails to function properly.

When we need run fsck command in Linux

  • The system automatically finds system inconsistent usually after a system crash or power loss or after unclean unmount.
  • System fails to boot
  • When the system has I/O error
  • Schedule to run fsck for filesystem intergerity on boot or every few months

how to use fsck command in Linux

For our purpose, let’s assume that we have already identified the problematic device /dev/sdb.

First, we need to make sure that the drive has been unmounted:sudo umount /dev/sdb
Now run the fsck command: sudo fsck /dev/sdb
Check the output for any errors. If none displayed, check the exit code with echo $?.

5 Fsck command Examples in Linux

There is a list of options that are available with the fsck utility for specific purposes. Some of the useful fsck options are:

  • Perform fsck dry run – This performs a test run. fsck -N /dev/sdc
  • Run non-interactively – answers yes to all questions, this will avoid all prompts. fsck -y /dev/sdc
  • Just Print fsck Error to Stdout without repair. fsck -n /dev/sdc
  • Run fsck on all filesystems: fsck -AR. The -R will skip the root filesystem as its cant be unmounted on a running machine
  • Run fsck for a specific filesystem. The fsck command is a wrapper and internally uses the respective filesystem checker command (fsck.*). You can find the following various fsck checker commands such as fsck.ext2, fsck.ext3, fsck.ext4, etc.).

# cd /sbin
# ls fsck*
fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.minix fsck.xfs

fsck command Options in Linux

  • -A Check all file systems present in /etc/fstab
  • -C Display progress bar
  • -f Check filesystem forcefully
  • -l Lock the device
  • -M Do not check mounted filesystems
  • -N Print output without executing any actions
  • -P Check multiple filesystems parallelly
  • -p Automatically repair any issues that can be safely resolved without the need for user interaction
  • -R Do not check the root filesystem when used with -A
  • -r Print statistics for each device that has been checked
  • -T Do not show the Title
  • -t Specify the filesystem types to be checked (This can be done using man command)
  • -v Provide a detailed output
  • -y Assume ‘yes’ to all the questions

Understanding fsck exit codes in Linux

After running fsck, it will return an exit code. These cods can be seen in fsck’s manual by running:

# man fsck
0 No errors
1 Filesystem errors corrected
2 System should be rebooted
4 Filesystem errors left uncorrected
8 Operational error
16 Usage or syntax error
32 Checking canceled by user request
128 Shared-library error