Home Terminal Tuts Check hard disk for Bad Sectors by command-line in Ubuntu, Linux Mint, and elementary OS

Check hard disk for Bad Sectors by command-line in Ubuntu, Linux Mint, and elementary OS

Published: Last Updated on
scan bad sectors in hard disk

Hard disk failures are just a thing that’s bound to happen to every computer. But, time of complete failure is something that you should estimate based on the scan results. Presence of bad sectors is the beginning of the end of a hard disk drive. Bad sectors are hardware related and can’t be fixed. You can only monitor it and make your OS to not use the bad sectors for writing data.

In this session of Terminal Tuts, let’s learn how to find out the presence of bad sectors and errors in your computer’s hard disk. We had already published the GUI method of finding SMART status and errors using ‘Disks’ utility – just FYI.

Scan for Bad Sectors and Errors on the hard disk in Ubuntu, Linux Mint, and elementary OS

Note that if you want to scan your computer’s internal hard disk which is mounted, you should be using an Ubuntu Live USB drive and boot into it. Then launch ‘Terminal’ from the Live environment and follow these steps. If you are checking an external hard disk, you need to see that it is not mounted.

Step 1: First, let’s use the fdisk command to find out the hard disk partitions status.

sudo fdisk -l
fdisk command output

fdisk command output

You should see a few entries of RAM. For example,  Disk /dev/ram15 seen in my test PC. You can ignore it as this is the RAM disk driver used by main system memory as a block device.

In the above screen-shot example, /dev/sda is the hard disk of size 465.8 GB that I’m interested in scanning.

Step 2: Next, let’s find if there are any Bad Sectors on the hard disk. We shall use badblocks command. Make sure to enter your hard disk info instead of /dev/sda in below command. My test PC has /dev/sda for the hard disk. This command will scan for bad blocks in the hard disk and then export the result to the file badsectors.txt in the ‘scan_result’ directory.

sudo badblocks -v /dev/sda > /scan_result/badsectors.txt

Step 3: Finally we shall use fsck command to tell Ubuntu not to use the bad sectors mentioned in the badsectors.txt file. That way life of the hard disk is increased a bit until you get a new one for replacement.

sudo fsck -l /scan_result/badsectors.txt /dev/sda

You may also like

18 comments

mellatweb March 11, 2017 - 2:34 AM

Thank you update your site

Reply
parity n February 14, 2018 - 6:46 AM

how long did it take to scan your 465.8 GB drive? i.e.run step 2 from start to finish.

I started it on a 1.5Tb external drive and it ran for over 30 minutes (without any clue how long was left), so killed it. Can we run it with the ‘-s’ option (to show progress bar)?, if so, does it give a good indication of progress?

Reply
Kiran Kumar February 17, 2018 - 9:15 PM

Sorry, I don’t remember how long it took, but generally badblocks command is not the fastest. It takes several days sometimes to even complete a 1TB hard disk. 30 minutes is too short in your case. You can use -s to show progress bar. The command format for it: badblocks -s device. All the best!

Reply
parity n February 27, 2018 - 9:31 PM

I ran the badblocks test on my 1.5TB external usb drive with the -s switch. It gives % complete and time elapsed (in seconds). Once it hit 1% I looked at the time elapsed. It said 138 seconds, so I multiplied this by 100 = 138000, then divided by 60 (to get minutes), again divided by 60 (to get hours) = 3.8 hours.

Left it running this time. It actually took around 4 hours to complete. So with the -s switch you can get an idea/estimate (early on in the process) how long it will take to complete.

Here’s my output:

test@test:~$ sudo badblocks -v -s /dev/sdc > ~/testbadblocks-ext-hdd.txt
Checking blocks 0 to 1465105407
Checking for bad blocks (read-only test): 0.00% done, 0:00 elapsed. (0/0/0 errdone
Pass completed, 0 bad blocks found. (0/0/0 errors)

The process was pretty resource hungry. Don’t know if running it in the background/low priority will make it less resource intensive. (probably take longer to complete) Something to ponder.

Reply
QuikMaths April 22, 2019 - 6:17 AM

138 seconds is just over 2 minutes… I don’t know what kind of maths you applied but if the elapsed time said 138 seconds that means that it took 2 minutes and 18 seconds to reach a progress of 1%.

Reply
marc November 21, 2020 - 9:07 PM

True, but he said it was at 1%. He used normal maths. I don’t know what kind of reading you applied.

Rajarshi Paul May 15, 2018 - 11:13 AM

Hello Sir,
I cannot access my hard drive partitions on my Ubuntu OS. I have a 500Gb Hard disk size which is not accessible or rather even visible with the Ubuntu OS. Any suggestions?

Reply
Ingo May 24, 2018 - 3:43 PM

sudo fsck -l < /scan_result/badsectors.txt /dev/sda

Reply
Ali July 5, 2018 - 3:24 AM

Dear Kiran, Thank you for this article. it’s really helpful. there’s only one problem (maybe 2) . When I run the fsck command, I receive the following error messages:
fsck: the -l option can be used with one device only — ignore
e2fsck: need terminal for interactive repairs
any idea what can be done about them?

Reply
Henrique Picelli November 22, 2018 - 8:30 AM

Hi there, just a question. If my hard drive shows 0 bad sectors, should i tell Ubuntu the output file or it is not necessary?
Best regards,
Henrique

Reply
Mustafa January 8, 2019 - 8:57 AM

Hello, I have a new laptop with a new SSD hard disk and I don’t have ‘SDA’ name for my hard disk partition. Since new SSDs are using NVMe drivers the name of partitions comes such as “nvme0n1p1”. I cannot use these kinds of names with smart or any other application so far. Disks program also does not give me anything because the “self-test” button is inactive.

Do you know how can I get information about the health of my hard disk with NVMe drivers?

Reply
Kammua July 26, 2020 - 12:54 PM

Dear Mustafa,

SSDs do not need to be checked for bad sectors. They are not mechanical drives. If they start to fail it is because of flash memory’s limited rewrite potential. A SSD that is failing has run it’s usable lifespan, and needs to be replaced immedeately. Perhaps SSDs of the future will use memory technologies with indefinite rewrites. But until then it’s best to replace them about every 5 years, before they show signs of failure.

Reply
John August 5, 2019 - 3:57 PM

Dear Kiram,

Thank your for sharing this information. I would suggest you to use:

sudo badblocks -vs /dev/sda > /scan_result/badsectors.txt

because it takes some time to finish the task and “-vs” (specially the “s”) give you a update of the task completed.

Reply
Christophe NAUD September 17, 2019 - 12:27 PM

christophe@christophe-ESPRIMO-E5731:~$ sudo badblocks -v /dev/sdb1 > /scan_result/badsectors.txt
bash: /scan_result/badsectors.txt: No such file or directory

Reply
Sanaul October 25, 2021 - 3:27 AM

I have same problem

Reply
UncleMonkeyPants November 23, 2021 - 6:38 AM

the ‘ > /scan_result/badsectors.txt ‘ part means “write the results to this file”. You’re getting that error because the directory /scan_result does not exist, so use a different directory, like ~/Desktop/badsectors.txt or something

Reply
Andy March 14, 2020 - 5:30 PM

If you get :
badblocks: Value too large for defined data type invalid end block (7814026240): must be 32-bit value

Then add -b 4096
e.g.
sudo badblocks -b 4096 -vs /dev/sdb > ~/Desktop/badsectors.txt

Andy

Reply
Vitalicsu March 13, 2021 - 10:50 AM

fsck from util-linux 2.34
fsck: the -l option can be used with one device only — ignore
e2fsck: need terminal for interactive repairs

Reply

Leave a Comment

fl_logo_v3_footer

FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews, FOSS Linux is the go-to source for all things Linux. 


Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY

ALL RIGHTS RESERVED.

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.