The fsck command in Linux: check and repair file systems

Introduction

In the Linux world, filesystem integrity is essential to ensure that the operating system runs without errors and that stored data is not corrupted. One of the most important tools for maintaining that integrity is fsck, short for “file system check”. This command allows checking and, if necessary, repairing inconsistencies in various types of filesystems.

What is fsck?

fsck is a command-line utility that is run before mounting a filesystem or when problems are suspected after an unexpected shutdown, power failures, or hardware errors. Its main function is to examine the internal structure of the filesystem, look for lost blocks, corrupt inodes, or damaged directory entries, and apply the appropriate corrections according to the options given to it.

Supported filesystems

The fsck command acts as a wrapper that invokes the specific checkers for each type of filesystem. Therefore, it works with ext2, ext3, ext4, XFS, Btrfs, FAT, NTFS (via tools such as ntfsfix) and many more. Each checker has its own name, such as e2fsck for ext* or xfs_repair for XFS, but fsck takes care of calling the appropriate one automatically.

When to run fsck

It is recommended to run fsck in the following scenarios: after an abrupt system shutdown, when error messages appear while mounting a partition, if disk performance noticeably degrades, or as part of a scheduled preventive maintenance. On server systems, it is common to schedule a monthly or quarterly check depending on the criticality of the data.

Most used options

  • -a or --auto: automatically repair without prompting.
  • -y: assume “yes” to all repair questions.
  • -n: read-only mode, shows what would be done without applying changes.
  • -f: force the check even if the filesystem appears clean.
  • -C: show a progress bar during the operation.
  • -t type: specify the filesystem type when fsck does not detect it automatically.

Step-by-step to use fsck safely

  1. Boot the system into rescue mode or from a Live CD/USB to avoid mounting the partition to be checked.
  2. Identify the target partition with lsblk or fdisk -l.
  3. Unmount the partition if it is mounted: umount /dev/sdXn.
  4. Run the command in test mode first: fsck -n /dev/sdXn.
  5. Review the output; if errors are reported, repeat with the repair option: fsck -y /dev/sdXn.
  6. Once finished, remount the partition and reboot the system normally.

Precautions and best practices

Although fsck is powerful, its incorrect use can lead to data loss. Therefore, always make a recent backup before intervening in a critical partition. Use the -n mode to inspect without changes, and only when you are sure of the necessary corrections, apply the repair options. On journaling filesystems like ext4 or XFS, the probability of corruption is lower, but fsck remains useful for detecting hardware problems or inconsistencies that the journal cannot resolve.

Conclusion

fsck is an essential tool in any Linux administrator’s arsenal. Knowing its operation, the available options, and the appropriate moments to run it allows maintaining filesystem health, minimizing downtime, and protecting information integrity. Incorporating periodic fsck checks into maintenance plans ensures a more stable and reliable environment.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .