If you have a file with sensitive information, it may not be enough to simply delete it with the rm command or press the Delete key. Deleting a file with the rm command usually only removes it from our directory. The deleted file remains on disk and can be recovered and abused by an attacker with the necessary skills.

In Linux, you can use the shred command to safely delete the files by repeatedly overwriting the file with gibberish data. This makes recovery of the original data quite difficult or almost impossible, even if the deleted file is recovered. The Shred command not only overwrites a file but also deletes it if specified. You can also use it to overwrite partitions or an entire disk.

In this article, we explain how to use the shred command in Debian 11 to safely delete files. The same procedure can be used in other Debian and Ubuntu versions. We also discussed some of the command line options.

Working with Shred Command

Shred command is one of the GNU Core Utilities and is available on nearly any Linux system including Debian. Let’s see how to work with the shred command:

Shred Command Syntax

Following is the shred command syntax:

$ shred option <FILE>

Where the “FILE” can be a file or any hard disk partition.

When you use the shred command without any option, it overwrites the file with gibberish data multiple times. To understand what the shred command does, let’s create a test file named “testfile.txt” with some text in it.

$ echo “this file contains some sample text” > testfile.txt

This is what our example file looks like:

Example test file

After creating the file, also check the size of the file. We will use it later to compare it against the size of the shredded file.

$ ls -l testfile.txt

Test file size

Now run the shred command (without any command line option) followed by the file name that you want to shred.

$ shred testfile.txt

The above command will overwrite the testfile.txt three times (by default). To see what happened to the test file, call the cat command:

$ cat testfile.txt

From the cat command output, you will only see the gibberish inside the file.

Securely deleted file

Also, if you view the file size, you will notice it has increased.

File size after using shred

Shred Command Line Options

The shred command has a few command line options to allow you to expand its functionalities. Let’s have a look at some examples of how these options work.

Verbose Output

Using the -v or –verbose option, you can view what is happening in the background.

$ shred -v testfile.txt

The following output shows the three passes of overwriting the file with the random numbers.

Shred verbose

Note: Next for all the following examples, we will use the -v option for displaying the output.

Overwrite Multiple Files

If you have more than one file, you can shred them using a single command instead of shredding them one by one using separate commands. To shred more than one files, type them all as an argument (separated by space) or use the wildcard character to specify all the files which have the same extensions.

$ shred -v testfile1.txt testfile2.txt testfile3.txt

All three files will be shredded in a single process as shown in the following screenshot.

Delete multiple files using shred

Overwrite Drives

You can also use the shred command to overwrite the drives and partitions. For example, to overwrite all the data on the /dev/sda2 partition, the command would be:

$ sudo shred -v /dev/sda2

delete and overwrite partition using shred

Overwrite with zeros

Usually, the shred command overwrites the file with random data. However, it will be conspicuous on your system that the shredding operation was performed on this device. You may hide the shredding process using the -z or –zero option with shred command.

Using the shred command with -z or –zero option first overwrites the file with random numbers, then adds a final overwrite with zeros.

$ shred -vz testfile.txt

zero overwrite

In the above output, you can see that after overwriting the file three times with random numbers, the shred finally overwrote the file with zeros.

Selectively Overwrite

The shred command overwrites the files 3 times with random junk. To increase the number of overwrite passes, use the -n or –iterations option.

For example, to shred the testfile.txt using 5 number of overwrite passes, the command would be:

$ shred -vn5 testfile.txt

Selective overwrite

Overwrite Only First x Bytes

The default behavior of the shred command is to overwrite the whole file. Using the -s or –size option with the shred command allows you to overwrite only first x bytes. For instance, to overwrite only the first 6 bytes of testfile.txt, the command would be:

$ shred -vs6 testfile.txt

The above command will only overwrite the first 6 bytes of the specified file. You can verify it by calling the cat command.

Overwrite first bytes only with shred

Delete File after Overwriting

As discussed earlier, the shred command only overwrites the file if we use it without any command line options. However, after overwriting, you can delete the file as well using the -u or –remove option with the shred command. Note that it will also rename the file before deletion.

$ shred -vu testfile.txt

Delete file after overwriting it with random data

From the above output, you can see that the file was finally removed after being overwritten and renamed.

View help

To find more details about the shred command, use the –help option or visit the man page:

$ shred --help

Or

$ man shred

In this article, you have learned how to use the shred command in Debian along with various command line options. You have seen how the shred command overwrites and deletes the files, making them hard to recover using any recovery tools.

How to Securely Delete Files Using Shred Command in Debian 11

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn