PUBLISHED ON: FEBRUARY 16, 2023
How to use shred command to Overwrite a File to Hide Its Contents, and Optionally Delete It in Linux
In Linux, if we want to overwrite a file and hide its contents and optionally delete it, we use the shred
command line tool. Once the file will be deleted using the shred
command it is harder for even any expensive hardware probing to recover the data.
- It overwrites a file to hide its contents, and optionally delete it.
- Shred command is a part of GNU Core Utilities.
- It can be invoked either on ordinary files or on devices, which are represented as files in Linux/Unix.
- By default, shred also shreds file slack(unused space in file allocations).
The General Syntax of the shred command
shred [OPTION]... FILE...
Brief description and cheat sheet of options available with the shred command.
Options |
Description |
-f , --force |
change permissions to allow writing if necessary. |
-n , iterations=N |
Overwrite N times instead of the default(=3) |
--random-source=FILE |
get random bytes from FILE |
-s , --size=N |
shred this many bytes (suffixes like K, M, G accepted) |
-u |
deallocate and remove file after overwriting |
-v , --verbose |
show progress |
--help |
display this help and exit |
--version |
output version information and exit |
Overwrite a file's contents and make it unrecoverable with shred
command
If we just want to use it to overwrite a file, then all you gotta do is to execute shred
with a file name as argument.
Overwrite a file and delete a file as well with shred
command in Linux
In this example, we can see that textfile.txt
is available in the current directory without using the command (with -u
option) but after using the command file textfile.txt
is removed.
Execute shred
command with verbose mode and display output that how many times the file is overwritten
In this example, after using the -v
option with shred command it will display every time it overwrites the file and the process will be prompt.
Add a file overwrite with zeros to hide shredding in Linux
In this example, using the -z
option with the shred command file is overwritten with null. We can see it outputs nothing in textfile.txt
after executing the command.
Conclusion
In this tutorial, we covered how to overwrite a file to hide its contents, and delete it using the shred
command in Linux-based operating systems with available options and suitable examples. Be careful while using the shred command because it is harder for even any expensive hardware probe to recover the data.