Command Line Methods to Completly Delete a Hard Drive in Linux

Here are 2 methods to completly dlete the content of an hard drive with dd and shred commands. With those methods you won’t be able to recover the data. This can be useful in case you want to sell,  throw away your hard drive / computer or have doubtful activities.

If the partitions you want to delete are system (boot) partitions you’ll need to start your system with a live CD / USB such as SystemRescueCD or GParted.

Finding the location of a drive or partition

In order to know the exact path for your drive, you can use the fdisk command as root or sudoer:

# fdisk -l

Disk /dev/sda: 1887.4 GB, 1887436800000 bytes
255 heads, 63 sectors/track, 229467 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          25      200781   83  Linux
/dev/sda2              26      229335  1841932575   83  Linux
/dev/sda3          229336      229465     1044225   82  Linux swap / Solaris

If the example above, the drive /dev/sda is found with 3 partitions and a 1.8 TB capacity in total.

Deleting a drive with dd

To delete an hard drive with dd, you can use the following command that fill the drive /dev/sda with zero on every sector:

dd if=/dev/zero of=/dev/sda

If you only want to delete one partition, add the partition number:

dd if=/dev/zero of=/dev/sda1

and if you want to format all partitions of all drives, you can concatenate commands. For example (for 2 drives):

dd if=/dev/zero of=/dev/sda; dd if=/dev/zero of=/dev/sdb;

Most of the time, these commands take a lot of time and no progress is shown. But there is a trick. You just have to use dcfldd command that does the same thing as dd but display the number of megabytes processed:

# dcfldd if=/dev/zero of=/dev/sda
6144 blocks (192Mb) written…

Deleting a drive with the shred command

The method above may be considered insufficient for the most paranoid among us. It is recommended to repeat the procedure several times to be sure that none of recovery procedures fail. shred utility allows to do this easily and it is available on SystemRescueCD.

If you want to install it on a Debian based distribution, you can use the following command:

sudo apt-get install coreutils

or for an RPM based distribution such as Fedora:

sudo yum install coreutils

Here’s an example of shred which by default does 25 pass!:

# shred -z -v /dev/sda
shred: /dev/sda: pass 1/4 (random)…
shred: /dev/sda: pass 1/4 (random)…235MiB/38GiB 0%
shred: /dev/sda: pass 1/4 (random)…1.8GiB/38GiB 4%
shred: /dev/sda: pass 1/4 (random)…2.5GiB/38GiB 6%
shred: /dev/sda: pass 1/4 (random)…3.5GiB/38GiB 9%

where:

  • -z add a final overwrite with zeros to hide shredding
  • -v show progress

 

You could also add “-n 50” to overwrite 50 times instead of the default (25).

Adapted from Tux Planet (In French).

Share this:
FacebookTwitterHacker NewsSlashdotRedditLinkedInPinterestFlipboardMeWeLineEmailShare

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK Pi 4C Plus

2 Replies to “Command Line Methods to Completly Delete a Hard Drive in Linux”

Leave a Reply

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

Khadas VIM4 SBC
Khadas VIM4 SBC