Badblocks is the command-line utility in Linux like operating systems which can scan or test the hard disk and external drive for bad sectors. Bad sectors or bad blocks is the space of the disk which can’t be used due to the permanent damage or OS is unable to access it.
Badblocks command will detect all bad blocks(or bad sectors) of a hard disk and save them in a text file. Later we can use it with e2fsck to instruct Operating System(OS) not to store any data on these damaged sectors or blocks.
In this article, we will learn how to check or scan hard drive for bad sectors using badblocks command.
Step 1) Use fdisk command to identify hard drive info
Run fdisk command to list all available hard disks to Linux operating system. Identify the disk which you want to scan for bad blocks.
$ sudo fdisk -l
Step 2) Scan hard drive for Bad Sectors or Bad Blocks
Once you identify the hard disk then run badblocks command. Example is shown below
$ sudo badblocks -v /dev/sdb > /tmp/bad-blocks.txt
Just replace “/dev/sdb” with your own hard disk / partition. When we execute above command a text file “bad-blocks” will be created under /tmp , which will contains all bad blocks.
Step 3) Inform OS not to use bad blocks for storing data
Once the scanning is completed, if the bad sectors are reported then use file “bad-blocks.txt” with e2fsck command and force OS not to use these bad blocks for storing data. Run the following e2fsck command
$ sudo e2fsck -l /tmp/bad-blocks.txt /dev/sdb or $ sudo e2fsck -c /dev/sdb
Note : Before running e2fsck command, you just make sure the drive is not mounted.
For any further help on badblocks & e2fsck command , read their respective man pages
$ man badblocks $ man e2fsck