July 2, 2022

Tutorial: Repair A Linux File System on Linux Using Fsck

Fsck(file system constantly check) is a utility that’s normally found on Linux operating systems. It checks for damaged data and attempts to repair the file system. With this tutorial, you will learn how to use fsck on your drives.

Find your Device

The first thing you want to do is check where the damage partition is located and if it’s mounted. By using lsblk you can see where the storage device file is located. (Note: The Device file contains the instance of a piece of hardware on your computer. These files are located in the /dev folder on the filesystem.)

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 223.6G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 223.1G  0 part /
sdb      8:16   1  59.6G  0 disk 
└─sdb1   8:17   1  59.6G  0 part 

I see that sdb is 59.5G and it’s close to the size of my USB stick and it’s not mounted. Right below that we’ll find sdb1, this is a partition type that we can mount and intend to scan. The device file is at /dev/sdb1, Take note of where the mount point is if it’s there.

You’re going to need to know what kind of filesystem it is. Using another utility called fidsk, you will be able to find this out. Running the command “sudo fdisk -l” will show all the drives attach to your system and their partitions.

$ sudo fdisk -l

Disk /dev/sdb: 59.62 GiB, 63999836160 bytes, 124999680 sectors
Disk model: USB 3.0 FD      
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xab612de1

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 124999679 124997632 59.6G  b W95 FAT32

fdisk will show much more information on the devices on your computer than lsblk. Here my device model is called USB 3.0 FD and at the bottom, it shows a partition located at /dev/sdb1 formatted with FAT32 type.

But be sure to Unmount the drive before using Fsck. Since if you don’t the partition would be damage. Use the umount command with the mount-point or device file location. Note: You may need to run the command with sudo.

#at the device file
$sudo umount /dev/sdb1
#or at the mount-point
$sudo umount /mnt/usb 

Run Fsck

Depending on the filesystem of your partition you will need to run a different command for it. We know that my USB is fat32, the command that I would use is fsck.fat.

CommandFilesystem type
fsckext2,ext3,ext4, most Linux filesystems
fsck.fatfat32

To Run the fsck command with the path to the device file. You will need to run the command with sudo.

#if the filesystem is ext2,ext3,ext4
$sudo fsck /dev/sdb
fsck.fat 4.1 (2017-01-24)
/dev/sdb1: 1 files, 1/1952609 clusters

#if the filesystem is fat32
$sudo fsck.fat32
fsck.fat 4.1 (2017-01-24)
/dev/sdb1: 1 files, 1/1952609 clusters

Checking The Root Filesystem

Since unmounting the root system is not possible. You Check the root file system by making a file at root called forcefsck and rebooting. This can be done with the command touch with sudo. fsck will run on the next reboot.

#touch will make a blank file called forcefsck at the root dirctory /
$sudo touch /forcefsck
$reboot

Alternatively, you can use a Live Linux USB drive and scan your main drive with that environment. You can learn to make one here.

colby

Computer guru with years working with technology. I find it fun to tinker with computer new and old, and make them do my work for me.

View all posts by colby →

Leave a Reply

Your email address will not be published.