How to repair corrupted filesystem on Linux?

The Linux filesystem can be corrupted due to system power failure, system crashes, and unsafe removal of drives. This can cause problems like your system may fail to boot or a partition cannot be mounted. After such an incident it is recommended to repair or at least check the filesystem to prevent data loss on a system.

In this article, we will use the fsck command to check and repair corrupted filesystem on a Linux computer.

Linux fsck command

The fsck (filesystem check)command on Linux is used to perform consistency checks and interactive repair of a filesystem. It is one of the most useful commands on Linux.

The syntax of how to use the fsck command on Linux is given below.

fsck [Options] [Filesystem]

You always need to have root permissions to run this command. And you can find the detailed list of options on the fsck command man page.

How to repair a corrupted filesystem

Always unmount the partition before running the fsck command otherwise it can damage the filesystem. If your system is able to boot and have the access to the terminal then you can follow the given steps to repair a corrupted non-root filesystem.

Run the following command to display all the partitions and their information –

sudo fdisk -l

Here identify the correct partition where you want to run the fsck command.

Suppose /dev/sda6 is your /home partition and you want to perform a filesystem check on it. First, you need to unmount this partition, use the given command to unmount it –

sudo umount /dev/sda6

Now use the following command to automatically repair the filesystem –

sudo fsck -p /dev/sda6

Here option -p will instruct the command to fix the issue without user intervention.

Once done mount this partition again by using –

sudo mount /dev/sda6

How to repair corrupted root filesystem

If the root partition is corrupted probably you will not be able to access your desktop. You cannot unmount the root partition on a running machine. In this case, you need Linux live media to fix this issue.

First, restart your computer and boot using live media. Now on your terminal run the following command to list the partitions and identify your root partition name.

sudo fdisk -l

Suppose /dev/sda5 is your root partition first unmount it with the following command –

sudo umount /dev/sda5

Now run the following command to fix any inconsistency in your filesystem –

sudo fsck /dev/sda5

This will interactively ask you to press y to clear the buffer or any other fixes. Read and confirm it till it completes its work.

Once done reboot your system using –

reboot

Remove the live media when it prompts you to boot into your system.

Conclusion

I hope this fixes the issue with the filesystem on your Linux computer. If you have any query then leave them in the comments below.

Leave a Comment