Linux Commands

How to Securely Erase a Disk and File Using the Linux shred Command

shred is a Linux command line utility that enables users to erase a disk and file from the terminal. It overwrites the data in the file or a whole disk with random bits and removes it securely from the system. This command makes it harder to recover the data using third-party software. The shred command removes the file by passing it through three passes and in these three passes, this command ensures the file is overwritten three times. You can change the number of passes by using the -n parameter.

This article is a detailed guide to securely erase a disk and file via the Linux shred command.

How to Securely Erase File using the Linux shred Command

In Linux, there are multiple commands for removing the files including rm and rmdir. However, the shred command is a different one that overwrites the files and makes them unrecoverable. The basic syntax of the shred command is given below:

shred [OPTION] filename

If you just want to overwrite the test file, execute the following command:

shred testfile

If you want to remove the file then, use the -uvz flag with the shred command and name of the file, -u will remove the file after overwriting, the -v shows the progress and -z performs the overwriting:

shred -uvz [/file/system/path]

In the below example, I am removing the document1 from my system:

shred -uvz document1

By default, the shred command overwrites the file 3 times, you can change this behavior by using the -n with the count for overwriting the file:

shred -n 4 -v <filename>

Securely Erase a Disk Using the Linux shred Command

If you are selling your drive then you can remove the data from it using the shred command so that no one can access the previously deleted data from the drive. You can use the -v to get the detailed output, -f forces the overwrite, and -z writes the zeros in the final pass and removes or erases all the data from the hard disk or USB drive.

sudo shred -vfz /dev/sdb

Note: Replace the /dev/sdb with the name of your device, which can be found through “lsblk” command.

Bottom Line

shred is an important command-line tool for Linux systems as it deletes the data on the disk in a secure fashion so that no one can re-access the data from the disk. The tool is preinstalled on most Linux distributions including Debian and you can see the shred command use with different examples provides the above-mentioned guidelines.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.