Delete files securely in Linux

Posted on Monday June 20, 2011 / by Eric Potvin

Deleting files or empty your trash bin does not really remove your file on the disk. It simply just delete the reference for the file in the master boot record so next time a file is either created or update it can use the space.

This is good if you accidentally deleted file you didn't want to delete like bank records or password list. On the other hand, if you sell your computer or just your hard drive and you want to make sure nobody has access to your personal data you need to "shred" them.

To make sure the data is unrecoverable by anyone, it needs to be overwritten. Shred let you delete files (or your entire hard drive) permanently by overwriting the files with with random data. Not only it destroy the content of the files but also make them impossible to recover.

Shredding files:

shred -z -u -n200 /home/MyUser/Documents/Bank/*

-z overwrite with zero's the last time, to mask the shred process. This means the content of the file will ONLY contains zero (0)
-u means delete when you're done overwriting
-n200 means overwrite the file(s) 200 times

What about files with spaces?

Sometimes the shred command will not work with files or folders with spaces. The simple way to bypass this issue is to use the find command.

find -type f -exec shred -n1 -u -v {} \;
Wait! my folders are still there

Once all files are shredded, you can remove folders using the srm command.

srm -r *

Shredding Hard Drive

It is recommended to boot from a LIVE CD since the first time the data is overwritten it might crash your system.

shred -z -n10 /dev/sdb1

The -u option cannot be used since you cannot shred the device.