/ command line

How to safely erase files on Linux

If you delete a file with rm filename it won’t be visible to you any more but the actual content of the file will still be on the hard drive and could be recovered.  To securely remove files so that no one can unerase them you can use the shred utility.  Shred is not effective on every file system but it works on standard ext3/ext4 file systems that use the data=ordered (the default) and data=writeback modes.

To securely erase one file:
shred -fuvz filename<br></br>
To remove multiple files:
shred -fuvz filename1 filename2<br></br>
To erase a folder including all files and subfolders:
find /path/to/folder -print0 | xargs -0 shred -fuvz<br></br>

If the last command gives you the error message* xargs: argument line too long* you can use the similar but a bit slower command
find /path/to/folder -type f -exec shred -fuvz '{}' \;

The -print0 parameter and the -0 behind xargs help Linux to deal with file names that contain spaces. The fuvz parameter behind shred takes care that file permissions are changed if necessary (-f), the file will not only be shredded but also removed afterwards (-u), you will see which file is currently getting erased (-v) and after the file has been overwritten with random data it will be overwritten another time with zeros so that it is not obvious the file has been shredded (-z).

How to safely erase files on Linux
Share this