2

I have a large drive (4TB) that I've only written a few gigabytes to. I'm giving it to a friend, so I'd like to zero it out. I know I can zero out the whole drive with dd, but I'd like to know if:

  1. zeroing only the non-zero bits will be faster, and
  2. if so, how to go about doing that.

EDIT: I trust my friend and there's nothing sensitive on the drive anyway, so no worries if zeroing isn't a bullet-proof method of erasing.

11
  • Simple answer, overwrite it with anything meaningless, then delete it all. Dec 3, 2023 at 21:59
  • 2
    The sectors that are non-zero could be in-use sectors or deleted sectors, and could be anywhere on the drive. There is no efficient method of finding every non-zero sector other than a search by actual reading. Simply overwriting without bothering to read would actually be faster. Anything less than overwriting every sector would be a half-baked job that could leave some sectors untouched. Since you "trust your friend", you could simply delete all the partitions, or reformat all partitions. Then there are no directories or files to look at unless an intentional recovery is attempted.
    – sawdust
    Dec 4, 2023 at 1:12
  • Note that it's not only about trusting your friend. It could also involve trusting whomever they give/sell the drive to on a future date, ad infinitum. That said, you mention that there's nothing sensitive on the drive, so as sawdust mentioned, simply deleting or reformatting the partitions may be sufficient. Dec 4, 2023 at 1:20
  • @sawdust I agree with most of what you wrote, except that if most of drive is empty, and we are dealing with an HDD, reading the whole thing and only writing over non-zero sectors is likely much faster than writing over the entire drive. This is because writing is a much slower operation than reading. And if we are dealing with an SSD, then filling the entire drive with random bytes is really the only choice. Of course, since the data isn't sensitive, your suggestion of simply deleting/reformatting partitions will likely be sufficient, and has the added benefit of being fast. Dec 4, 2023 at 1:26
  • 2
    @davidgo "and we could find/write a program to do this" – Already done: superuser.com/a/1722580/432690 Dec 4, 2023 at 9:18

1 Answer 1

2

Zeroing out non-zero bytes is likely to be error prone and not significantly faster - indeed it's likely to be slower and you will need to read the whole disk to determine what needs to be read.

If you are not overly concerned, and the disk is fairly new (i.e. does not have a lot of fragmentation), you may want to try only overwriting the first X gigabytes of the drive. You can do this with dd and limiting the number of blocks written - but I might be inclined to use pv and just write until I am tired of it (pv shows how much has been piped through it - so is a reasonable proxy of how much has been cleared). I might use a command like:

pv < /dev/zero > /dev/sdX

You can use crtl + c to stop.

1
  • 3
    (1) pv < /dev/null exits immediately. Do you mean /dev/zero? (2) Please review "you will need to read the whole disk to determine what needs to be read". Dec 4, 2023 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .