6 Unix / Linux Badblocks Command Examples to Check Disks and Partitions

badblocks command is used on a disk or partition to check whether any bad blocks are present. The following are few examples that will help you to identify bad blocks.

Warning: Do not perform this command if you don’t know what you are doing. Combining this command with other disk related command can be dangerous and erase all your data. So, try this command only on a test system for learning purpose.

1. Basic Usage of badblocks

When you execute badblocks command on a disk partition, depending on the size of the disk, it might take a long time to complete. Execute this command only on a partition that is not mounted.

By default, it doesn’t display any output on the screen, when there are no bad blocks as shown below.

# badblocks /dev/sda2

To see what the command is doing, and how much scanning it has done so far, use the badblocks -v option as shown below.

# badblocks -v /dev/sda2
Checking blocks 0 to 8388607
Checking for bad blocks (read-only test):  12.78% done, 0:14 elapsed

When the test is completed, it will display the output as shown below.

# badblocks -v /dev/sda2
Checking blocks 0 to 8388607
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found.

2. Specify Block Size

By default it uses 1024 as block size. You can specify a block size using -b option as shown below.

The following example uses 2048 as block size to scan the /dev/sdb3 disk.

# badblocks -v -b 2048 /dev/sdb3
Checking blocks 0 to 332
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found.

Note: Use tune2fs command to view the block size of your partition before you run the badblocks command.

3. Specify Maximum Bad Blocks Count

By default the badblock command will keep going until the test is finished. But, the following example will stop doing further checks once it find 100 bad blocks.

# badblocks -v -e 100 /dev/sda2
Checking blocks 0 to 8388607
Checking for bad blocks (read-only test):  12.78% done, 0:14 elapsed

4. Skip Known Bad Blocks

If you already know certain blocks are bad, and you want the badblock command to skip these, create the following badblocks.txt file with those known bad blocks.

# cat badblocks.txt
24447
24497

Next, use -i option and pass the above file name. This will instruct badblocks command to skip these blocks for testing.

# badblocks -v -i badblocks.txt /dev/sda2
Checking blocks 0 to 8388607
Checking for bad blocks (read-only test): ^C5.48% done, 0:17 elapsed

5. Write the Badblocks to a File

If you know a disk has lot of bad blocks, you can instruct badblocks command to write those to an output file using -o option as shown below.

# badblocks -v -o badblocks.log /dev/sdb3
Checking blocks 0 to 665
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found.

# cat badblocks.log

6. Perform a Badblock Write Mode Test

Warning: Don’t do this!

Ok, you read the warning above. right? This will erase the data on the disk.

By default badblocks command will perform read-only test. The -w option shown below will perform a write-mode test to identify bad blocks. This will write some patterns to the disk, and read it to see whether it is able to read it successfully.

# badblocks -vw /dev/sdb3
Checking for bad blocks in read-write mode
From block 0 to 665
Testing with pattern 0xaa: done
Reading and comparing: done
Testing with pattern 0x55: done
Reading and comparing: done
Testing with pattern 0xff: done
Reading and comparing: done
Testing with pattern 0x00: done
Reading and comparing: done
Pass completed, 0 bad blocks found.

Comments on this entry are closed.

  • Anonymous October 26, 2013, 7:27 am

    Hi, if your HDD is a all out failure OS is already wiped but still reads and writes to a certain extent but has other malfunctioning issues, I would advise the badblocks -vw /dev/sda1