How To Erase Disk In Linux And Mac Using Simple Commands?
Short Bytes: Sometimes you need to erase a disk that isn’t working properly or just needs to be wiped for security reasons. But how do you do that without the all the fancy tools you can find online? Well, if you’re a Mac or Linux user, it’s very easy to do using tools like dd and shred (Windows, not so much, but still doable).
I don’t know how many times I’ve needed to wipe a disk that was acting up and just reformat. In a home that uses Windows, Linux, and Macs, there’s always some computer complaining about how some disk is not formatted properly.I’ve come across a few scenarios when drives were so deranged that my go-to tools, like gParted (and Disk Utility before they butchered all the features and only left the bone), would produce errors every time I tried to format or create a new partition table. It’s pretty frustrating when some of the best tools simply don’t want to work. What do you do then to securely erase disk in Linux and Mac?
Recommended: Learn About Partition Tables Here
The best way to move forward is to erase the drive. Many tools can reliably secure-erase drives, albeit painfully slowly. If that’s not what you need, what do you do when your normal drive tools are still behaving wonky? To erase drives with ease and speed, you need to go back to the basics.
NOTE: SSDs require much more care when erasing. Because of the compression techniques used inside SSDs, zeroing-out a drive (writing only zeros) will produce a drive that looks like it has been wiped, but has not been wiped. Read the manual for your SSD if you need to securely wipe it, chances are, the manufacturer has provided a tool or very specific instructions.
Erasing disk in Linux and OS X using dd command
The first tool, and arguably one of the most important to erase disk in Linux and Mac, is dd, (Windows versions are available, but the use is slightly different). dd command for wiping drive is like a data scalpel. It allows you to cut out pieces of data in precisely the size you want from precisely where you wanted it, and then place it where you want it, all while providing a bunch of different options. Its syntax is archaic but simple.
dd if=/dev/zero of=/dev/sda bs=1M count=1
Here we use four flags:
- if – This is the input file, we’ve used /dev/zero to produce zero or null bytes.
- of – This is the output file, our disk.
- bs – This is the block size (not super important in most cases).
- count – This is the number of blocks that we copy.
The above command will copy one megabyte of zeros to the disk. That would erase the partition table of the drive and allow a partitioning program to create the table and some partitions.
Alright, what if you want to securely erase an entire drive? Well, you can still do that with dd, all you need to do is change your input from /dev/zero to /dev/random or /dev/urandom and then run it through however many passes you believe is sufficiently secure. A for loop is handy for this drive wiping process, else you would need to intervene manually and repeat it as necessary, hardly efficient.
for i in $( seq 0 2 ); do dd if=/dev/urandom of=/dev/sda; done
We’ve omitted the bs and count options because when writing through a whole disk, they are unnecessary. dd will stop when it encounters the end of the file, the end of the file being the end of the disk. You could even accomplish this with cat as well.
cat /dev/urandom > /dev/sda
Alternatively, you can use dcfldd, which supports all that dd does and much more, including (most importantly?) displaying the amount of data copied.
BONUS TIP: Apart from using dd to erase disk in Linux and Mac, you can use it to write an ISO to a drive by specifying the ISO as the input file to create a bootable USB disk with ease. Additionally, you can use a disk as the input file and point to a filename that doesn’t exist to create an image of your disk.
Also Read: The Ultimate A To Z List of Linux Commands
Erasing disk in Linux and Mac using shred
The next option to erase drive, which is available on both Linux and Mac, though not always installed by default on Linux, is shred. It is very easy to use.
shred -n 3 /dev/sda
Running this command to wipe hard drive is essentially the same as our dd command in a for-loop. shred takes information from /dev/urandom and uses it to overwrite the disk (or file) specified. The -n option specifies the number of passes to make. In my experience, shred tends to run faster that using dd to read directly from /dev/urandom, but it does not benefit from the incredible ubiquity that dd does.
There are many other tools too that can be used erase disk in Linux and Mac, but they aren’t always within arm’s reach, especially on systems that are command line only. Windows has its own command line tools for disk manipulation, but tread cautiously, they are not as reliable as they might seem. I’ve found myself needing to create a partition table several times in diskpart before Windows would acknowledge it as a usable disk.
When in need of an even more secure way to erase drive, follow whichever of the above solutions you prefer, and then remove the platters from the drive and destroy them in your preferred method. That is about as secure as it gets, though it’s likely overkill for just about anything other than nuclear warhead codes.
What are your favorite methods to erase disk in Linux and Mac? Let us know in the comments below.
Also Read: 10 Most Dangerous Linux Commands You Should Never Run