Home Linux Commands How To Use Fsck Command To Check And Repair Linux File Systems (2024 Update)

How To Use Fsck Command To Check And Repair Linux File Systems (2024 Update)

Fsck Commands in Linux (20 Examples for Quick Reference)

By sk
631 views

Maintaining the integrity of file systems is crucial for keeping your Linux systems running smoothly. File systems can become corrupt or inconsistent due to hardware failures, power outages, or software bugs. This can lead to data loss or system instability. This comprehensive tutorial provides useful fsck commands with examples to check and repair file systems in Linux.

The fsck (file system check) utility helps you check and repair issues with your file systems. You can run it manually or have it automatically check file systems during system boot. With fsck, you can scan for errors, fix corrupt data structures, and mark bad disk blocks to prevent further issues.

Let's start with the basics of checking and repairing standard Linux file systems with fsck command. But before that, we need to find the file systems that need to be repaired and unmount them.

[NOTE]
The following commands must be run with sudo or root permissions.

Unmount File Systems

Using fsck on a mounted file system is generally not recommended. Running fsck on a mounted file system can lead to data corruption or other issues because the file system is actively being used by the operating system.

The safest way to use fsck is to unmount the file system first. You can use the df -h or lsblk commands to get your mounted file systems (Partitions). Refer this guide to list disk partitions in Linux.

Once you found the file system you want to repair, unmount it like below:

umount /dev/sdXN

Replace /dev/sdXN with the actual device identifier (e.g., /dev/sda).

After repairing the file systems, do not forget to mount them.

mount /dev/sdXN

Linux fsck Commands to Repair Linux File Systems

In this tutorial I have provided 20 essential fsck commands to check and repair file systems in Linux and Unix-like operating systems.

These commands are essential tools for Linux system administrators to maintain the integrity and reliability of file systems. They allow for checking, repairing, and managing bad blocks, as well as performing various other file system maintenance tasks.

It's important to use these commands with caution, as they can potentially cause data loss if used incorrectly. Always create backups before performing any file system repairs or modifications.

1. Dry Run File System Check

fsck -N

This option instructs fsck to perform a dry run, meaning it will not actually make any changes to the file system. Instead, it will display what actions it would take if executed without the -N flag. This is useful for previewing potential changes before committing them.

Sample Output of the above command would be:

fsck from util-linux 2.36.1
[/usr/sbin/fsck.ext4 (1) -- /] fsck.ext4 /dev/nvme0n1p2 
[/usr/sbin/fsck.vfat (1) -- /boot/efi] fsck.vfat /dev/nvme0n1p1 
[/usr/sbin/fsck.exfat (1) -- /media/user/WD_SSD] fsck.exfat LABEL=WD_SSD 

Allow me explain what this output means:

fsck from util-linux 2.36.1

This line indicates that the fsck command is part of the util-linux package, version 2.36.1.

[/usr/sbin/fsck.ext4 (1) -- /] fsck.ext4 /dev/nvme0n1p2

This line shows that fsck has detected an ext4 file system on the device /dev/nvme0n1p2 (likely a partition on an NVMe disk). In this dry run, fsck would call the /usr/sbin/fsck.ext4 command to check and potentially repair this ext4 file system mounted at the root directory (/).

[/usr/sbin/fsck.vfat (1) -- /boot/efi] fsck.vfat /dev/nvme0n1p1

Here, fsck has found a FAT (vfat) file system on the device /dev/nvme0n1p1, which is likely the EFI System Partition (ESP) mounted at /boot/efi. In the dry run, fsck would invoke /usr/sbin/fsck.vfat to check and potentially repair this FAT file system.

[/usr/sbin/fsck.exfat (1) -- /media/user/WD_SSD] fsck.exfat LABEL=WD_SSD

This line indicates that fsck has detected an exFAT file system with the label WD_SSD, mounted at /media/user/WD_SSD. In the dry run, fsck would call /usr/sbin/fsck.exfat to check and potentially repair this exFAT file system.

In summary, this output shows that fsck -N is performing a dry run to check three different file systems on this system:

  1. An ext4 file system mounted at the root directory (/).
  2. A FAT (vfat) file system mounted at /boot/efi, likely the EFI System Partition.
  3. An exFAT file system with the label WD_SSD mounted at /media/user/WD_SSD.

The dry run simply lists the actions that fsck would take to check and repair these file systems if run without the -N option. No actual changes will be made to the file systems during this dry run.

2. Check and Repair All File Systems

fsck -As

This command checks and attempts to repair all file systems listed in the /etc/fstab file. The -A option tells fsck to check all file systems, and the -s option instructs it to skip checking file systems marked as "clean."

3. Force File System Check

fsck -f /dev/sda1

This command forces a file system check on the /dev/sda1 device, which is typically a partition or disk. The -f option forces fsck to check the file system, even if it is marked as clean.

Example:

$ sudo fsck -y /dev/sda1
fsck from util-linux 2.36.1
exfatprogs version : 1.1.0
/dev/sda1: clean. directories 2774, files 40775

4. Force File System Check with Verbose Output

fsck -fv /dev/sda1

Similar to the previous command, this forces a file system check on /dev/sda1, but with the added -v option, which produces verbose output, providing more detailed information about the checking process.

5. Automatic File System Repair

fsck -y /dev/sda1

This command checks and repairs the file system on /dev/sda1, and the -y option automatically answers "yes" to any prompts or questions that fsck may ask during the repair process.

6. Check ext2 File System and Mark Bad Blocks

fsck.ext3 -c /dev/sda1

This command specifically checks an ext3 file system on /dev/sda1. The -c option instructs fsck to run the badblocks command to find and mark bad blocks on the device.

7. Mark Bad Blocks for ext2/ext3/ext4 File Systems

e2fsck -c /dev/sda1

This is similar to the previous command but for ext2/ext3/ext4 file systems. It marks all bad blocks found and adds them to the bad block inode, preventing them from being allocated to files or directories.

8. Automatic Repair for ext2/ext3/ext4 File Systems

e2fsck -p

This command automatically repairs an ext2/ext3/ext4 file system without asking any questions, making it suitable for unattended or scripted repairs.

9. Dry Run for ext2/ext3/ext4 File Systems

e2fsck -n

This option performs a dry run, similar to fsck -N. It checks the file system but does not make any changes, only reporting potential issues.

10. Assume "Yes" for ext2/ext3/ext4 File System Repair

e2fsck -y

Like fsck -y, this command assumes "yes" to all questions asked during the file system repair process, allowing for unattended or scripted repairs.

11. Check and Add Bad Blocks to Bad Block List

e2fsck -c

This option checks for bad blocks on the file system and adds them to the bad block list, preventing their future use.

12. Force File System Check for ext2/ext3/ext4

e2fsck -f

This forces e2fsck to check the file system, even if it is marked as clean.

13. Verbose Output for ext2/ext3/ext4 File System Check

e2fsck -v

Enables verbose output, providing more detailed information about the checking and repair process.

14. Use Alternative Superblock Location

e2fsck -b $Superblock

This option allows you to specify an alternative superblock location for e2fsck to use when checking the file system.

Example:

e2fsck -b 32768 /dev/sda1

This command checks the ext2/3/4 file system on /dev/sda1 using the superblock located at byte offset 32768. This can be useful if the primary superblock is corrupted, and you need to use an alternate superblock location.

15. Force Block Size for Superblock Search

e2fsck -B $BlockSize

Forces e2fsck to use a specific block size when searching for the superblock.

Example:

e2fsck -B 4096 /dev/sdb2

This command forces e2fsck to use a block size of 4096 bytes when checking the file system on /dev/sdb2. This can be necessary if the file system was created with a non-standard block size.

16. Set External Journal Location

e2fsck -j $Dir

Specifies the location of an external journal for the file system being checked.

Example:

e2fsck -j /mnt/backup/external_journal /dev/sdc1

This command specifies that the external journal for the file system on /dev/sdc1 is located at /mnt/backup/external_journal. This is relevant for file systems that use an external journal, such as when using a separate disk or partition for the journal.

17. Add Bad Blocks from File to Bad Block List

e2fsck -l $BadBlocksFile

Adds the bad blocks listed in the specified file to the file system's bad block list.

Example:

e2fsck -l /root/bad_blocks.txt /dev/sda2

This command adds the bad blocks listed in the /root/bad_blocks.txt file to the bad block list for the ext2/3/4 file system on /dev/sda2.

18. Set Bad Block List from File

e2fsck -L $BadBlocksFile

Sets the bad block list for the file system being checked to the blocks listed in the specified file.

Example:

e2fsck -L /root/bad_blocks.txt /dev/sdb1

This command sets the bad block list for the ext2/3/4 file system on /dev/sdb1 to the blocks listed in the /root/bad_blocks.txt file, overwriting any existing bad block list.

19. Find and Report Bad Blocks

badblocks -o $BadBlocks.rpt /dev/sda3 $TotalBlockCount

This command finds and reports bad blocks on the /dev/sda3 device, writing the results to the $BadBlocks.rpt file. The $TotalBlockCount parameter specifies the total number of blocks to check.

Example:

badblocks -o /tmp/sda3_bad_blocks.rpt /dev/sda3 10485760

This command scans the device /dev/sda3 for bad blocks and writes the results to the /tmp/sda3_bad_blocks.rpt file. It will check a total of 10485760 blocks (5 GB for a 512-byte block size) on the device.

20. Force File System Check with Bad Block List

e2fsck -f -l $BadBlocks.rpt /dev/sda1

This command forces a file system check on /dev/sda1, using the bad block list provided in the $BadBlocks.rpt file generated by the badblocks command.

Example:

e2fsck -f -l /tmp/sda3_bad_blocks.rpt /dev/sda1

This command forces a file system check on /dev/sda1 and uses the bad block list contained in the /tmp/sda3_bad_blocks.rpt file (generated by the badblocks command in the previous example). This can be useful if you want to check or repair a file system using a pre-existing bad block list.

fsck vs e2fsck

As you may noticed in the previous examples, I have used both fsck and e2fsck commands. Are they similar? You might wonder.

No, fsck and e2fsck are not the same, although they are related and serve similar purposes.

fsck (short for "file system check") is a generic command that can be used to check and repair various types of file systems on Linux and Unix-like operating systems. It acts as a front-end for different file system-specific checking tools, such as e2fsck for ext2/ext3/ext4 file systems, xfs_repair for XFS file systems, fsck.vfat for FAT file systems, and so on.

When you run fsck without specifying a particular file system type, it tries to detect the file system type automatically and then calls the appropriate file system-specific checking tool. For example, if you run fsck on an ext4 file system, it will automatically invoke e2fsck in the background.

e2fsck, on the other hand, is a specialized tool designed specifically for checking and repairing ext2, ext3, and ext4 file systems. It is a lower-level utility that performs more detailed and comprehensive checks and repairs on these types of file systems.

In summary, the key differences between fsck and e2fsck are:

  1. fsck is a generic front-end command that can handle various file system types, while e2fsck is specific to ext2/ext3/ext4 file systems.
  2. fsck automatically detects the file system type and calls the appropriate file system-specific tool (like e2fsck for ext2/ext3/ext4).
  3. e2fsck provides more advanced and detailed options for checking and repairing ext2/ext3/ext4 file systems, as it is designed specifically for these file system types.

So, while fsck is a general-purpose tool, e2fsck is a specialized tool for ext2/ext3/ext4 file systems.

If you know you are working with an ext2/ext3/ext4 file system, you can use e2fsck directly for more granular control and options.

Otherwise, fsck serves as a convenient front-end for checking and repairing various file system types.

fsck Commands Cheatsheet

Here's a cheatsheet for essential fsck (file system check) commands in a neat tabular format:

CommandDescription
fsck /dev/sdXNCheck the file system on the specified partition (e.g., /dev/sda1).
fsck -ACheck all file systems listed in /etc/fstab.
fsck -NShow what would be done without actually doing it.
fsck -CDisplay progress bar while checking.
fsck -MSkip mounted file systems.
fsck -RSkip the root file system.
fsck -VVerbose mode, shows detailed output.
fsck -yAutomatically answer 'yes' to all prompts.
fsck -nAutomatically answer 'no' to all prompts.
fsck -rInteractively repair the file system.
fsck.ext4 /dev/sdXNSpecifically check ext4 file system on the partition.
fsck -t ext4 /dev/sdXNCheck file system of specified type (ext4 in this case).
fsck -lLock the device to prevent multiple checks.
Table 1: fsck Commands Cheatsheet

Replace /dev/sdXN with the actual device identifier (e.g., /dev/sda1).

These commands cover the basics and some advanced options for using fsck to maintain and repair file systems.

e2fsck Commands Cheatsheet

Here's a cheatsheet for essential e2fsck commands:

CommandDescription
e2fsck /dev/sdXNCheck the ext2/ext3/ext4 file system on the specified partition (e.g., /dev/sda1).
e2fsck -p /dev/sdXNAutomatically repair the file system without any questions.
e2fsck -n /dev/sdXNDo not make any changes to the file system, only check it.
e2fsck -y /dev/sdXNAssume "yes" to all questions.
e2fsck -f /dev/sdXNForce check, even if the file system seems clean.
e2fsck -c /dev/sdXNCheck for bad blocks and add them to the bad block list.
e2fsck -v /dev/sdXNVerbose mode, show detailed output.
e2fsck -b 32768 /dev/sdXNUse an alternative superblock (e.g., 32768).
e2fsck -B 4096 /dev/sdXNSet the block size to 4096 bytes.
e2fsck -C 0 /dev/sdXNDisplay a progress bar.
e2fsck -j /dev/sdXNCheck a file system with an external journal.
e2fsck -D /dev/sdXNOptimize directories in the file system.
e2fsck -E <extended-options> /dev/sdXNSpecify extended options (see man page for details).
e2fsck -k /dev/sdXNPreserve the bad block list.
e2fsck -r /dev/sdXNInteractively repair the file system (same as without options).
Table 2: e2fsck Commands Cheatsheet

Replace /dev/sdXN with the actual device identifier (e.g., /dev/sda1).

These commands cover the basics and some advanced options for using e2fsck to maintain and repair ext2, ext3, and ext4 file systems.

Conclusion

Remember, a healthy file system is crucial for maintaining system stability and data integrity. Regularly checking and repairing your file systems with fsck is an essential part of your Linux system maintenance routine.

By effectively using the fsck command and the examples provided in this guide, you'll be well-equipped to diagnose and repair common file system issues on your Linux system.

We have also included essential cheatsheets for fsck and e2fsck commands for quick reference. Keep them near your desk always and use them when necessary.


Related Read: An Introduction to Linux /etc/fstab file


You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More