I'm working on fixing the errors in my Hard Disk, It's possible to do that manually but it might take hours.

After typing this command sudo badblocks -b 512 /dev/sda, I got hundreds of results and I want to avoid fixing them manually.

More details about my error can be found here

So is there any tool I can use to fix the errors? Or maybe you could help me write a shell script?


Well, there are a couple of cases:

  1. This disk is part of a RAID array. Good. Just have md 'repair' the array like this: echo 'repair' > /sys/block/md0/md/sync_action. Problem solved without data loss. (I'm guessing this isn't the case for you, but you really ought to consider changing that.)
  2. You don't care about the data on the disk (or there isn't any). Just use dd to zero the whole disk.
  3. The bad blocks are part of the free space on the disk. Use e.g., cat /dev/zero > tempfile to fill the free space with zeros. Do this as root (there is space reserved for root only), probably in single-user mode (so nothing breaks from running out of space). After that runs out of space, remove the file (rm tempfile).
  4. The bad blocks are part of the data (files) or metadata (filesystem structure) on the disk. You have lost data. fsck -fc (run with the filesystem unmounted, or worst case in readonly during early boot if its the root filesystem) will tell you which files. Replace them from backup.

Its also possible that badblocks -n, which must only be done on an unmounted filesystem, will force a remap. It shouldn't lose any data (other than what was in the bad blocks, which is already lost).

If you want to script it based on the badblocks output (which is not safe, it leaves you with silent corruption), that's fairly easy. Each line of badblocks output gives you a block number, based on your block size (512 in your example). Use the same block size for dd's bs. The block number is your seek for dd. Your count is 1 (or higher, if there are a few bad blocks in a row). of is the partition (or disk) you ran badblocks on. A good if is /dev/zero.

  • I think No.3 is my case otherwise i need new HDD.. – Aziz Al-ghannam Dec 2 '11 at 16:04
  • after filling the free space, how can i remove the tempfile now? – Aziz Al-ghannam Dec 2 '11 at 17:01
  • @AzizAl-ghannam: using rm, like rm tempfile. – derobert Dec 2 '11 at 17:02
  • 0 Bad sectors, You are the best!! – Aziz Al-ghannam Dec 2 '11 at 17:07
  • people, how can cat /dev/zero > tempfile fix bad blocks??? – Dims Mar 26 '16 at 17:45

e2fsck -c -c seems to do the same thing, with block size insurance.

  • why the negatives here? man e2fsck says that -c does the same thing – arivero Jan 23 '20 at 14:26

go into single user mode and do fsck -yvf /dev/sda

It seems like, it does need an fsck after all.

  • I'd be cautious about running fsck -y personally. Also, this won't get the sectors remapped, or even noted in the filesystem as bad (you need -c for that). – derobert Dec 2 '11 at 15:32

I am not sure if you can do this with a single command because I have seen some script solution on internet.

The script below finds bad sectors, puts bad blocks results into a text file, then if text file size is different than zero, e2fsck will mark bad sectors (so these marked sectors will not be used by operating system).

for disc in `fdisk -l | grep '^/' | awk '{ print $1 }'`; do
   badblocks -v $disc > $target
   if [ -s $target ]; then
       echo "badblock(s) found on $disc"
       e2fsck -l $target $disc
       echo "no badblocks on $disc"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.