The fsck command in Linux: check and repair file systems

Introduction

In the Linux world, file system integrity is essential to ensure that the operating system works without errors and that the stored data is not corrupted. One of the most important tools to maintain that integrity isfsck, abbreviation «file system check». This command allows you to review and, if necessary, repair inconsistencies in various types of file systems.

What is fsck?

fsck is a command line utility that runs before mounting a file system or when problems are suspected after unexpected shutdown, power failures or hardware errors. Its main function is to examine the internal structure of the file system, search for lost blocks, corrupt inodes or damaged directory entries and apply appropriate corrections according to the options indicated.

Compatible file systems

The fsck command acts as a container that invokes the specific verifiers of each type of file system. Therefore, it works with ext2, ext3, ext4, XFS, Btrfs, FAT, NTFS (through tools like ntfsfix) and many more. Each verifier has its own name, as e2fsck for ext* o xfs _ repair for XFS, but fsck is in charge of calling the right one automatically.

When to run fsck

It is recommended to run fsck in the following scenarios: after a sudden system shutdown, when error messages appear when mounting a partition, if the performance of the disk decreases significantly, or as part of a scheduled preventive maintenance. In server systems, it is common to schedule a monthly or quarterly review according to the critical data.

Most commonly used options

  • -aor--auto: automatically repairs without asking.
  • -y: assumes «Yes.» to all repair questions.
  • -n: mode only reading, shows what would be done without applying changes.
  • -f: forces the review even if the file system seems clean.
  • -C: shows a progress bar during the operation.
  • -t type: specifies the type of file system when fsck does not automatically detect it.

Step by step to use fsck in safe mode

  1. Start the system in rescue mode or from a Live CD / USB to avoid mounting the partition to be reviewed.
  2. Identify the target partition withlsblkorfdisk -l.
  3. Unmount the partition if mounted:umount /dev/sdXn.
  4. Run the command in test mode first:fsck -n /dev/sdXn.
  5. Check the output; if errors are reported, repeat with the repair option:fsck -y /dev/sdXn.
  6. Once finished, reassemble the partition and restart the system normally.

Precautions and good practices

Although fsck is powerful, its misuse may cause data loss. Therefore, always make a recent backup before intervening in a critical partition. Use the mode-nto inspect without change and, only when you are sure of the necessary corrections, apply the repair options. In systems with journaling such as ext4 or XFS, the probability of corruption is lower, but fsck remains useful for detecting hardware problems or inconsistencies that the journal cannot solve.

Conclusion

fsck is an essential tool in the arsenal of any Linux administrator. Knowing its operation, available options and the right time to run it allows you to maintain the health of file systems, minimize inactivity times and protect the integrity of the information. Incorporating regular fsck reviews into maintenance plans ensures a more stable and reliable environment.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish