Securely Deleting Files with Shred (with examples)
1: Overwriting a file
Code
shred path/to/file
Motivation
When we want to securely delete a file, simply deleting it from the file system does not guarantee that the data within the file is completely erased. The file may still be recoverable using specialized software or techniques. The shred
command provides a way to overwrite the file with random data, making it extremely difficult to recover the original data.
Explanation
The shred
command is used to securely delete files by overwriting their data. By running shred
followed by the path to the file, the command will overwrite the file’s data with random values.
Example Output
If we have a file called secret.txt
in the directory /home/user
, running shred /home/user/secret.txt
will overwrite the data in the secret.txt
file with random values, making it difficult to recover the original contents.
2: Overwriting a file with zeroes
Code
shred --zero path/to/file
Motivation
In some cases, we may want to overwrite a file with zeroes instead of random data. This can be useful when working with sensitive data that needs to be completely erased, or when preparing a file for compression or encryption.
Explanation
The --zero
option instructs the shred
command to overwrite the file with zeroes instead of random values. This can provide a more predictable and uniform overwrite pattern.
Example Output
Running shred --zero /home/user/secret.txt
will overwrite the data in the secret.txt
file with zeroes, effectively erasing the original contents.
3: Overwriting a file multiple times
Code
shred -n25 path/to/file
Motivation
To further enhance the security of file deletion, we can overwrite the file multiple times with random data. By increasing the number of overwrites, we decrease the chances of data recovery.
Explanation
The -n
option specifies the number of times the file should be overwritten. In this example, we are using -n25
to overwrite the file 25 times.
Example Output
Running shred -n25 /home/user/secret.txt
will perform 25 overwrites of random data on the secret.txt
file, significantly increasing the difficulty of recovering the original data.
4: Overwriting a file and removing it
Code
shred --remove path/to/file
Motivation
In some cases, we may want to securely delete a file and remove it from the file system in a single command. This can help prevent accidental recovery of the file.
Explanation
The --remove
option instructs the shred
command to remove the file after overwriting it. This combination of actions provides a convenient way to both securely delete and remove the file.
Example Output
Running shred --remove /home/user/secret.txt
will overwrite the data in the secret.txt
file with random values and then remove the file from the file system. The file will no longer be accessible or recoverable.