Check bad sectors on Linux

This is one of the many reason I love Linux. My desktop was acting up since a couple of days. I finally found I was having bad sectors on my hard drive.

Checking bad sector

badblocks is the command in linux which can scan or test hard diskfor bad sectors. Bad sectors or bad blocks is the space of the disk which can’t be used due to the permanent damage.  Badblocks  command will detect all bad sectors  on hard disk and save them in a text file so that we can use it with e2fsck to configure  OS not to store our data on these damaged sectors.

Step:1 Use fdisk command to find hard drive info

sudo fdisk -l
```Open new terminal for next step

Step:2  Scan your hard drive for Bad Sectors

sudo badblocks -v /dev/sda > ~/bad.txt

Just replace "/dev/sda"  with your own hard disk / partition. When we execute above command  a text file “bad.txt” will be created under /home , which will contains all bad blocks.

It will take a lot of time to finish execution of the command, depends on how many bad sectors your hard drive has. Just be patient, best to go have a coffee or something. One way to check its working is to see the last modification date/time of bad.txt. The text file generated will have number in it. They are bad sectors' location.



Step:3 Inform OS not to use bad sectors

Once the checking is completed , if the bad sectors are reported , then use file “bad.txt” with e2fsck command  and force OS not to use these bad blocks for storing data.

sudo e2fsck -l bad.txt  /dev/sda1

sudo e2fsck -l bad.txt  /dev/sda5

sudo e2fsck -l bad.txt  /dev/sda6

sudo e2fsck -l bad.txt  /dev/sda7

sda1, sda5,sda6,sda7 are the partitions on my hard drive, yours will be different, take reference from the fdisk -l command.

**Note** : Before running e2fsck command , you just make sure the drive is not mounted.