Repair sectors and recover a hard disk (HDD) in Linux

Hard Drives (or HDDs) we have already spoken here in DesdeLinux, we have shown you guides or tutorials on dd (very useful or disastrous terminal application, it depends how you use it hehe) and more, this time I will talk about how we can recover or repair those HDDs that we have with problems at home, that we have "discarded" in a drawer or we already forget in a box 😉

Btrfs

If any hard drive has bad sectors we can repair them with the tool badblocksThe first thing to do will be to know which is the hard drive we want to repair (/ dev / sdb… / dev / sdc… etc), for this we put the following in a terminal:

sudo fdisk -l

This will show us / dev / sda, its size in GBs and its partitions, equal to / dev / sdb if it exists, and so sdc and others depending on the amount of storage devices you have on your computer.

Suppose the hard drive in question is an external one and is / dev / sdb, then the command to start detecting and repairing bad sectors would be:

Related article:
Check if a file or folder exists or not (and more) with IF loop
The hard disk can NOT be mounted, under no circumstances can a partition of the hard disk in which they are going to work be mounted !!

badblocks -s -v -n -f /dev/sdb

  • -s: indicates that the process will be displayed with percent
  • -v: verbose mode, which means that it will show us the number of errors
  • -n: indicates that we will try to use a non-destructive mode, that is, that we will try to recover those sectors but also the information that was in them
  • -f: force reading and writing on devices that are mounted. Normally if the HDD were mounted badblocks would not check the sectors that are being used, but, as I already warned you and advised that you can NOT have the hard disk mounted, we will use the -f parameter to force the recovery of all possible sectors

It will take a long time, and I repeat, quite. It could easily last for hours or days depending on the size of the hard drive, how damaged it is, the speed of your computer, etc. So I recommend you leave the computer there quiet, without moving it for the necessary time, that and a lot of patience 😉

Is there any specific tool for ext2, ext3 or ext4?

To tell the truth, there is, it can be used e2fsck, they still have to know which partition is the one they want to check, suppose it is / dev / sdb1, then it would be:

e2fsck -p -v -y /dev/sdb1

  • -p: indicates an attempt to automatically repair the damage found
  • -v: verbose mode, that is, to show us the errors on the screen
  • -y: it will answer Yes to all the questions such as, do you want to recover X sector?

 The end!

Related article:
Establish network connection between the PC and Virtualbox virtual machines

Well, nothing more to add, before I used HirensBootCD, but I think that this option will prevent me from booting from another OS. I also bought a 1tb external hard drive on this web I use to save important files, or I do it in the cloud.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Lolo said

    There was something of a "destructive" recovery mode.

    This is useful when we want the OS to mark those damaged sectors as bad so that they are not used as long as we do not mind losing the information that is recorded. It may be that we have a damaged Swap partition, a disk that we have backup copy of or things like that.

    It is supposed to be more efficient at locating faulty areas than the NON-destructive mode, hence its usefulness, but I'm afraid it's been so long since I had to use this that I no longer remember how it was done.

    1.    rosgory said

      If I want to do that "destructive" recovery mode, is it done with a command using badblocks or with a CD like HirensBootCD?

      1.    Lolo said

        CAUTION: Be aware that destructive mode erases all data on the disk drive. That is why it is called like that, the only thing is that it better detects bad sectors and they are marked so that they will not be used again.

        I don't remember very well how he was a friend but I think it's badblocks -wsv / dev / sda1 (or whatever partition) I repeat, you can bundle it.

        You don't need a CD unless you want to run it on the root partition and don't mind reinstalling it all over again.

        If there are many bad sectors on your disk, it is best to buy another one since more will surely appear soon.

      2.    Lolo said

        The information is destroyed because the command writes information to each sector of the disk and then "reads" if that data has been recorded correctly.

        As the entire hard drive writes, all information is lost, while otherwise only read tests are done.

  2.   demo said

    One question, can you do the hard disk test on the mounted graphic desktop? . It has been days since the TABS of this page are working when clicking on forums or other tabs, very strange ... very strange, would it be that only I have been censored for something that they do not like? By the way, I have never said anything bad .

    1.    KZKG ^ Gaara said

      No, it is not recommended. The best thing is (if it is the only HDD on your PC) to boot through a LiveCD and then do the test.

      About lashes, nah, nobody banned you friend, how do you think? 😀 ... it was my mistake in the path to some files, that's why nobody (not me) worked for me, it's already corrected, Ctrl + F5 to refresh the cache and that's it.

  3.   Lolo said

    Don't worry about the lashes. They don't work for me either.

    It must be a fault of the web.

    1.    KZKG ^ Gaara said

      Indeed, it was an oversight of mine hehe, I already corrected it 😉

  4.   patodx said

    Good info, appreciated.
    Greetings friend KZKG ^ Gaara, you are well.

    1.    KZKG ^ Gaara said

      Duck man, how long 😀

      I hope all are well and go from strength to strength 😉

  5.   Juan said

    Thanks, let's see what's up

  6.   QUASAR said

    Thank you very much! I share it 😉

  7.   Rodrigo Molina said

    Thanks a lot. I only have one doubt left. How do you unmount the hard drive?

    1.    louis said

      With the umount command.

    2.    KZKG ^ Gaara said

      Here we explain how to mount and unmount the HDD: https://blog.desdelinux.net/como-montar-hdds-o-particiones-mediante-terminal/

  8.   Shadow said

    A question!!
    Would this be used for a pendrive or is it only for disks ???
    In addition to the question, what formats does badblocks support?

    1.    KZKG ^ Gaara said

      In theory it works for devices like pendrive the same, but, to the hardware being different ... well, I'm not sure if it will actually fix the device or not.

      About the formats, FAT, NTFS and EXT are the ones I know.

    2.    yukiteru said

      badblocks is used for both pendrives and hard drives, but it will not repair a flash memory that is degraded, since it is a physical damage that cannot be repaired.

  9.   Sabre said

    Anyway, if the disk throws you a Smart error, this doesn't save you, does it?

    1.    KZKG ^ Gaara said

      I will try to explain it simply and briefly 🙂

      When you buy a HDD of (for example) 500GB, we see that we can actually use (for example) 468GB, and we think that the other GBs that we lack have been stolen.

      The truth is that this is not the case, SMART is a "something" (system, etc.) that saves a part of the HDD and leaves the largest part for us, then, when some sector in the large section suffers damage, SMART changes that faulty for a new one, clean in the other part of the HDD, the one that is "hidden" or "missing".

      When we have problems with SMART, the vast majority of the time it means that SMART no longer has any more clean or non-corrupt sectors in its space reserved for it, that is, it cannot change one with problems for one without them as before. .

      Does this command solve life for us?

      Maybe if you fix enough sectors then SMART won't detect them as bad and (maybe) won't try to replace them (and show error on failure).

      Sorry if I made it too complicated, it is not a super simple topic to explain 🙂

      1.    eliotime3000 said

        In the case of the NTFS file system, the SMART system was previously used indiscriminately in Windows XP, causing various files to be fragmented down to the kernel components themselves.

        As of Windows Vista, since the demand on the NTFS file system has already been lowered, and since Windows 8.1, the ReFS file system (not ReiserFS) has to be counted for end users.

  10.   yukiteru said

    Badblocks, TestDisks and Smartmontools, how many times have these three tools saved my life, especially Badblocks and TestDisk, good tuto @KZKZ

    1.    KZKG ^ Gaara said

      You do what you can ... what, after 3 years writing, one is gradually running out of new or interesting material to share hahaha

      1.    eliotime3000 said

        If I had known about those tools, it would have saved me the trouble of using a disk defragmenter for my partition with Windows Vista.

      2.    Parakeet said

        Since you comment, to see if it serves as an idea for a new post:

        I never knew how to connect to a remote desktop in a way that doesn't interfere with the work the other user is doing on that other computer.

      3.    leo said

        Parakeet,

        You have to use XDCMP, in GDM you activate it in the configuration file /etc/daemon.conf as you can see in http://geroyblog.blogspot.com.ar/2013/06/using-gdm-and-xdmcp-with-remote-client.html?m=1

        On the dark side, you use remote desktop, RDP.

        Regards!

      4.    KZKG ^ Gaara said

        If you talk about connecting to another Linux, there a comment tells you about that.

        If you mean to connect to a Windows and do not bother the user, I am afraid that if it is not a Windows Server you will not be able to do it, the non-server editions of Windows do not allow it.

  11.   Hikari said

    These programs have ever saved my life to recover data from my old HDD, although usually they usually fail me mechanically and I have to put them in the freezer.

  12.   Rodrigo said

    Excuse my ignorance, but what do you mean by "The hard disk can NOT be mounted" and if it was mounted, how do I remove it.

    Thanks in advance for the answer you can give me.

    1.    KZKG ^ Gaara said

      Check if in / media / or in / run / media / there is a folder with the name of the HDD and if when entering it, you enter the HDD.

      If you can do that then it's mounted.

      To dismount it, it depends on the desktop you have (Gnome, KDE, etc). Generally from the File Explorer itself you have an icon in the sidebar that allows you to unmount or eject it.

  13.   Diego Campos said

    These tutorials are the ones that are really appreciated, very important ... direct to favorites: B

    Cheers(:

    1.    KZKG ^ Gaara said

      Thanks, you do what you can. It's been like 3 years publishing, it's difficult to find new interesting things to talk about 🙂

  14.   Mario G. Zavala said

    Excellent article ... but how it behaves when the failures are SMART.

  15.   Leo said

    Thanks for the tutor. It will be very useful to me, because in my job I have a stack of half-played hard drives that I can get some performance out of them. One question, the sectors that it cannot recover, what does it do with them? Does it separate or integrate them?

  16.   antonio said

    Good article, I have always liked everything closer to hard. I would like to know what difference it has with the specific programs of the manufacturers of the hdd. Once you start from them, they offer you a range of recovery possibilities, even a low-level format if necessary.
    And being specific to the brand of the hdd, they recertify it after recovering everything possible.

  17.   NauTiluS said

    Great article. In these last weeks that have passed, I have had to fight with two 1TB disks, with advanced format each one and well both with their respective bad sectors.

    I tried the badblocks a bit, but I realized that it took too long and did not have much time available to do it completely, since in my country, we suffer from power outages.

    Well, what I did was use the almighty god of bits, Mr. "dd."

    With this command, magic was possible.
    dd if = / dev / zero | pv | dd of = / dev / sdX bs = 100M
    The pv, is to show me where the zero padding was going on the disk.

    Thanks to that, I was able to figure out how many gigabytes the disk was failing, and using the fdisk command, I redone the partition tables.

    In one of the discs, it had a maximum loss of 9 gigabytes, something negligible for a 1TB disc.

    In the following image, you can see the final structure of the disc, and working perfectly, until now.

    http://i.imgur.com/9uvFhsb.png

    Greetings.

    1.    DAvid said

      Friend could you help me with a little more information, since I have the same problem with a 1tb disk

  18.   kevinjhon said

    like how much memory would you have to have to use the destructive option?

  19.   faustod said

    Regards,

    Thank you very much for your contribution, I hope you can cure me of this problem, a question that happens if I stop the process?

    Thank you
    faustod

  20.   David said

    The article is very interesting and above all to highlight what we should not do
    "The hard disk must not be mounted under any circumstances to be able to work on it"

  21.   Fernando said

    Excellent information! I would like to ask you what type of formatting is best if you want to store large files on a hard drive. I understand that it is ext4 but I'm not really sure.

    Thank you

    1.    Guido said

      Hey.
      Ext4 if you will only use it on Linux.
      NTFS if you will also use it in Windows.

    2.    yukiteru said

      Definitely XFS, I'm using it now and for both small and large files it's a bullet.

  22.   Jordy said

    Hi, I have a doubt.
    Currently on my computer I have 3 partitions on the hard drive, one with Windows, another with Ubuntu 14.10 and another to store the information that I need to have on hand on both systems.
    The problem is that for a few days when starting Ubuntu I get a message that my disk is damaged and I get some options to choose from, including repairing the hard disk.
    My question is, if I give it a repair, will I lose my Ubuntu data? or worse yet what about my other 2 partitions?

  23.   Erick said

    Hello .. Thanks for this valuable info!

    But I have a question. What happens if linux does not recognize my hard disk ?, that is to say; I have a 320 GB hard disk, and I have inserted several Linux Live CDs, this to use gparted or via command line format the disk and give it some utility, however, no live CD has recognized my hard disk , even with the command you put above ((sudo fdisk -l) the only thing it does currently is recognize a 16 GB USB which is where I have a debian installed with its respective partitions, but nothing from the 320 GB hard drive ... what I can do ?, I wish you could help me, otherwise I will send it straight to the trash.

    Thank you!

    1.    yukiteru said

      It sounds more like your hard drive is severely damaged. Have you checked if your disk is recognized by the BIOS of your PC?

  24.   jose said

    I have tried trying to recover the disk with the commands that tell me but I have not managed to recover the data. If this doesn't work, what else can I do? Thanks a lot

    1.    yukiteru said

      This is not for data recovery, but rather for recovering a hard drive and recognizing its bad sectors. To recover data, I recommend TestDisk and PhotoRec.

      https://blog.desdelinux.net/recuperar-archivos-borrados-facilmente-con-photorec-desde-la-consola/
      https://blog.desdelinux.net/como-recuperar-archivos-eliminados-de-una-tarjeta-sd/

  25.   Jesus said

    I have the hard drive mounted on my lap. In a USB memory I mount Ubuntu, where am I trying to do the whole process, the steps and commands are the same in my situation?

    1.    yukiteru said

      The steps are the same but you must bear in mind that the routes are different, since your hard disk can be recognized with a different dev, you will have to adjust that in due course.

  26.   Miguel said

    Hello, I followed this tutorial to fix my external hdd, which takes almost a month and I do not know when it will stop or if it will work, currently it goes through 193 million, the hdd is 1.5 tb and my pc is not very powerful
    http://imageshack.com/i/iddz316vj

    http://imageshack.com/i/eyrse3avj

    thanks 😉

  27.   winston montagne said

    Very good and interesting all

  28.   Juan contato said

    There may be topics on which I do not agree with you, but know this, you are always helping me when I need you !!!!! Thanks for this VERY USEFUL info !!!, And again THANK YOU !!

  29.   martin said

    hello good contribution yours. I have a small but big problem. I used the variant (-s -v -n -f -w) at 27 hours when I was testing the pattern (A055) the pc turned off.

    now i lost all partitions i am with the testdick but demomneto no luck.
    There was a partition that was working but when the pc turned off I cut the badblocks and I was left without it. do you have any idea how to solve this?

    It is a small 80gb disk, previously I was having problems with the smart, until system startup files were deleted and I got the badblocks. Well, if anyone knows, I've been with this problem for 3 days, I don't know where to go thanks bye

  30.   Luis said

    Greetings, I have tried to recover files from a 640 gb hard disk, it was drive c: of a lap, it worked with win7, the disk was damaged (I ignore the how, it is not mine), when connecting it as external, in windows to Sometimes it detects it and sometimes it does not, but it does not allow me to access it, I connect it to a pc with Linux, it detects it sometimes and sometimes it does not, the intention is to try to recover several data folders (mainly photo and video of a photographer) , I already recovered 56 gb of 280 gb which are the relevant ones, but I can no longer access the folders that I want to recover, if I insert the disk, I open folder by folder and when I get to a main one it tells me:

    Could not display all the content of «Photos xx»: Error getting information for file «/ media / pc / E83E5A7F3E5A472A / Documents and Settings / F / Documents / Photos xx / xx»: Input / output error

    I tried to use badblocks but it tells me:

    user @ team: ~ $ badblocks -s -v -n -f / dev / sdc
    badblocks: Permission denied while trying to determine device size

    What can I do to get that data back?

    Thank you in advance for your attention.

    1.    boulders said

      You tried with SUDO SU and then use the command?

  31.   Holger Precoma said

    Bro a question, I have Kali on a usb ready to try to repair the hard drive of a mini lap. The problem is that the hard disk does not detect me; is there any way to repair or locate it?

  32.   Jose Luis said

    Greetings bro. Very good information I had a while looking for how to repair a 20 GB hard drive with images and everything very well, just when I gave it to start the repair, I got "permission denied while determining the size of the device". Any suggestion for your timely help Thank you…. .

    1.    Anonymous said

      sudo su

  33.   Cesar Navarro said

    I have a problem, my W7 does not start for any reason, I have tried all safe modes but the problem persists, start the PC desde Linux Ubuntu 14.2 in the USB configuration I had to change the boot of the PC, when entering the hard drive from Linux the hard drive gives me this error:
    Error mounting / dev / sda3 at / media / ubuntu / eMachines: Command-line `mount -t" ntfs "-o" uhelper = udisks2, nodev, nosuid, uid = 999, gid = 999, dmask = 0077, fmask = 0177 »« / Dev / sda3 »« / media / ubuntu / eMachines »'exited with non-zero exit status 13: ntfs_attr_pread_i: ntfs_pread failed: Input / output error
    Failed to read NTFS $ Bitmap: Input / output error
    NTFS is either inconsistent, or there is a hardware fault, or it's a
    SoftRAID / FakeRAID hardware. In the first case run chkdsk / f on Windows
    then reboot into Windows twice. The usage of the / f parameter is very
    important! If the device is a SoftRAID / FakeRAID then first activate
    it and mount a different device under the / dev / mapper / directory, (eg
    / dev / mapper / nvidia_eahaabcc1). Please see the 'dmraid' documentation
    for more details

    What would be the next step to avoid losing my files?

    1.    Juan Gilberto Valerio Jacome said

      CESAR NAVARRO, did you find a solution to your problem? Thank you in advance

    2.    David salgado said

      I got access to the files by mounting the partition read-only.

      mkdir / media / windows
      sudo mount ntfs-3g -o ro / dev / sda4 / media / windows (In my case the data partition was sda4, but whatever)

      The problem is in the hiberfil.sys file. If you manage to erase it, you can mount the disk. And if you succeed, tell me how, because I have not been able.
      Technically erased with:
      sudo mount ntfs-3g -o remove_hiberfile / dev / sda4 / media / windows
      but it didn't work for me

  34.   Eric Xacon said

    Hello good, yesterday I had a problem with the removable hard disk and looking for information I found this website.
    The problem is that when I try to open the removable disk, it tells me that the disk is not mounted and does not let me open it.
    I have looked at which disk is with the function "sudo fdisk -l" and I have the disk is: Disk / dev / sdb.
    And with the function "badblocks -s -v -n -f / dev / sdb" it tells me that "permission has been denied in trying to determine the size of the device". I don't know what I can do, if someone can help me I would appreciate it a lot.

  35.   R. Carbajal said

    The same message appeared to me and I solved it by placing «sudo» just behind badblocks and putting my password

  36.   Large said

    Repair an NTFS?

  37.   manigold said

    Hello, the repair that is done is logical and not physical, I recommend backing up data, formatting the entire hard disk, with gparted leave the damaged hard disk space blank; unused, then partition to our liking and voila, linux will not bother you with startup problems or the typical "unable to copy, paste or whatever".

  38.   Ivan said

    Apparently the tool is quite useful.
    It takes about 6 days for a 320GB disk.
    How much longer could it take?
    Sure it's not great hardware where I'm running it.

    1.    Andrus Diaz said

      Hahaha ivan I miss the day I have half an hour waiting for 27% and he said that it took a long time you gave me illusions men >>>>

  39.   skintight said

    Excuse me, I ran bad blocks and I get pure lines like this, the detail is that I don't understand why the percentage is so large 62K. my HDD is 1Tb so I imagine it will take a long time.
    My theory is that it is 0% and 62640 blocks but the space is not marked.

    626400% done, 15:49:59 elapsed. (6097/0/0 errors)

  40.   SiriusDarkPy said

    Here is a problem with an HDD, Here in Venezuela HDDs are super stratoferically expensive, any help will be appreciated….

    The output of fdisk -l is

    fdisk -l

    Disk / dev / sda: 4013 MB, 4013948928 bytes
    255 heads, 63 sectors / track, 488 cylinders, total 7839744 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical / physical): 512 bytes / 512 bytes
    I / O size (minimum / optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00000000

    Device Boot Start End Blocks Id System
    / dev / sda1 * 128 7839743 3919808 c W95 FAT32 (LBA)

    Disk / dev / sdb: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors / track, 60801 cylinders, total 976773168 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical / physical): 512 bytes / 512 bytes
    I / O size (minimum / optimal): 512 bytes / 512 bytes

    The output of e2fsck is

    e2fsck -pvy / dev / sdb

    e2fsck: Only one of the options -p / -a, -n or -y may be specified.

    e2fsck -p / dev / sdb

    e2fsck: Bad magic number in super-block while trying to open / dev / sdb
    / dev / sdb:
    The superblock could not be read or does not describe a correct ext2
    filesystem. If the device is valid and it really contains an ext2
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193

    The output of badblock is

    badblocks -svnf / dev / sdb

    Checking for bad blocks in non-destructive read-write mode
    From block 0 to 488386583
    Checking for bad blocks (non-destructive read-write test)
    Testing with random pattern: 0.00% done, 0:10 elapsed. (0/0/0 errors)

    and when it goes like 0.04% the loquera hits it and says INVALID ARGUMENT DURING SEEK

    I appreciate any help….

  41.   Antonio Jose Yuste Lopez said

    I would recommend not using that program, the "badblocks" I have read that it has errors and in fact it left me a disk in raw mode and there was no way to recover anything and I say it with knowledge of linux and computer science, in fact I am computer scientist so great care…..

  42.   gordo9105 said

    Too bad you can't read the note, because the ads cover up the content and there's no way to remove them.

  43.   The Best Discs SSD said

    On this website you can find all the differences between HDD and SSD disks.

  44.   iamMacero said

    Such heavy and superb writing on this website.
    "The hard disk can NOT be mounted, under any circumstances blah blah blah"
    (And just below they put a command ...)
    "-F: force reading and writing on devices that are mounted."

    But they forget to say that it is better to back up the data and do a destructive deep scan (I will not tell you the command), it lasts much less and has better results. Or for example that badblocks is only to repair bad sectors, I REPEAT, only repair bad sectors. That, if these sectors cannot be repaired, it is necessary to isolate them by a tool that they obviously do not know.

    END

    PS: search the internet for the tool to isolate bad sectors and respond to the arrogant user because he cannot seem smarter than me.

  45.   Achilles Baeza said

    They should be ashamed of it, being a site where the use of FREE software is promoted and FORCES visitors to accept cookies, every day they look more like MIERDASOFT.