fsck - Linux


Overview

The fsck (File System Check) command in Linux is used for checking and repairing filesystem inconsistencies. It supports various filesystem types, such as ext2, ext3, ext4, and more. fsck is commonly used to examine filesystems after an unexpected shutdown or when there is a suspicion of corrupted files or directories.

Syntax

The general syntax for fsck is as follows:

fsck [options] [filesystem] [...]
  • [filesystem]: This is optional and can be a device name (like /dev/sda1), a mount point, or a filesystem label.

If no filesystem is specified, fsck will check filesystems listed in /etc/fstab.

Options/Flags

  • -A: Check all filesystems listed in /etc/fstab.
  • -C: Display a progress bar during the check (not available with all filesystem types).
  • -N: Show what would be done but do not actually execute.
  • -P: If checking multiple filesystems and this flag is specified, the filesystems will be checked in parallel to reduce total check time.
  • -R: Skip the root filesystem when combined with -A.
  • -T: Suppresses the printing of the title.
  • -V: Verbose mode, provides detailed output.
  • -a: Automatically repair the filesystem without any prompts (used only with some filesystem types).
  • -r: Provide an interactive repair prompt to the user.
  • -t: Specify the type of filesystem. This is useful when the filesystem type is not in /etc/fstab or when it has been changed.

Examples

  1. Check a specific filesystem and repair interactively:
    fsck -r /dev/sda2
    
  2. Check all filesystems listed in /etc/fstab with a progress bar:
    fsck -C -A
    
  3. Perform a dry run to see what fsck would do if executed:
    fsck -N /dev/sda1
    

Common Issues

  • Unmounted Filesystem: Always ensure that the filesystem is unmounted before running fsck. Using fsck on a mounted filesystem can result in data corruption.
  • Filesystem Clean Flag: Sometimes, fsck might refuse to run if the filesystem appears clean. Use -f to force a check.
  • Resource Usage: Checking large filesystems can be resource-intensive. Plan to run fsck during maintenance windows to avoid system performance issues.

Integration

fsck can be utilized in scripts executed at boot time to automate the checking of filesystems. Combined with cron jobs, it can be scheduled for regular checks:

0 2 * * 0 root /sbin/fsck -A -a

This cron job runs fsck every Sunday at 2 AM on all filesystems listed in /etc/fstab and repairs issues automatically.

  • e2fsck: Used specifically for ext2/ext3/ext4 filesystems.
  • xfs_repair: Tool for repair of the XFS filesystem.
  • badblocks: Used to scan a device for bad sectors.

For further reading and more detailed information, consult the man page for fsck by running man fsck on your system, or visit the online Linux manual pages.