How to Securely Erase a Disk or Flash Drive with Shred

To protect your privacy, it is always good practice to securely wipe data from your old and unused disk/flash drives in case you lose them, or get stolen, or if you simply wish to give them to someone else. Deleting your files (or formatting) won't do the job, since when you delete a file, it is removed from the filesystem index and the storage space is reclaimed, but the data is not actually destroyed. Unless the data is overwritten, you can easily recover them using undelete utilities.

Therefore, to properly clear off your data you'll have to use a specialized program, like the GNU shred. Before using shred you should find out the name of your device. If your device is a USB flash disk, the name might be something like /dev/sdb. You can use the GNOME Disk Utility for that purpose, which comes pre-installed on Ubuntu (as Disks):

GNOME Disk Utility

or, if you prefer the fdisk command:

$ sudo fdisk -l
Disk /dev/sdb: 3.8 GiB, 4009754624 bytes, 7831552 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
Disklabel type: dos
Disk identifier: 0xa5705b63

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1        2048 7831551 7829504  3.8G 83 Linux

After finding your device name, you can run shred with:

sudo shred -vz -n2 /dev/sdb

The above command will overwrite everything on your device with random data, 2 times (-n2), and then it will set all bits to zero (-z). Depending on the size of your drive, this might take a while to complete, so be patient. Keep in mind that everything on your device will be erased, including your partition table, so you will need to create new partitions before using the drive again.

You can read more about the shred command here.