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

Here’s how you can use the fsck command:

Open a terminal or console on your Linux system.

Identify the file system partition that you want to check and repair. This can be a device such as /dev/sda1, /dev/sdb2, or a mount point like /, /home, etc. Make sure you have the necessary permissions to run fsck on the partition.

To run fsck on an unmounted file system (recommended for system partitions), use the following command:

sudo fsck -t filesystem_type /dev/partition

Replace filesystem_type with the actual file system type of the partition (e.g., ext4, ext3, xfs, etc.), and /dev/partition with the device or partition you want to check. For example:

sudo fsck -t ext4 /dev/sda1

After running the fsck command, it will scan the file system, detect any errors, and prompt you to confirm repairs. You can respond to the prompts accordingly. It is recommended to read and understand the prompts before proceeding.

Once the repair process is complete, fsck will provide a summary of the actions taken. Review the output for any remaining issues or errors.

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