When you delete a file on your computer, the system clears the blocks in your memory by removing the reference to the file. The file doesn't suddenly go away and is still accessible with advanced software which may not be what you want.
Anyone with a file recovery tool can extract these deleted files from your storage and view their contents. But what if you don't want this to happen? And what's the best way to delete files on Linux so that no one can ever restore them?
This is where the shred utility comes in. This article explains in detail the Shred command, its limitations, and how to use it to securely delete files on Linux.
What is shredding and how does it work?
Shred is a command line utility that overwrites the contents of a file multiple times with random data to make it unrecoverable. It also allows you to delete the file after its data has been overwritten.
"Why is it overwriting the contents of the file?" You might ask. Let us understand it with an example. Keep in mind that you need to hide or "erase" content written on a piece of paper. Sure, you can crumple it up and throw it in the trash can. But anyone can take the crumpled paper ball, stand it up and read the contents.
On the other hand, a paper shredder cuts the sheet of paper into thin strips or pieces, making it nearly impossible to undo and view the contents.
This is exactly how the shred command works on Linux. It overwrites the contents of a file several times with a series of zeros so that no one can see the original contents. After that, it can safely remove the file from your system storage if you want.
When shouldn't Shred be used?
Note that shred does not work efficiently in all situations. According to the Shred man page, the utility is not effective when used on certain file systems. And these are:
Log structured or recorded file systems (ext3, XFS and JFS).
RAID-based file systems.
File systems that store snapshots.
File systems that store caches.
Compressed file systems.
The Shred man page also states that the command will only work with ext3 if it is in. is diary Mode. However, in the Data = write back and data = ordered Mode, the tool works like a charm.
Also, you shouldn't use the shred utility on SSDs as the extra erase and write process can damage your storage.
How to use the shred command
With shred, you can either overwrite and delete a file, or just overwrite the file without removing it.
The basic syntax of the command is:
File name of the shred options
…Where Options are the various flags used to invoke the methods of the command and Filename is the absolute or relative path to the file you want to edit.
Permanently delete a file
To permanently delete a file with Shred, use the -uvz Flag with the standard command.
- you: Deletes the file from memory
- v: Displays the output in verbose mode
- z: Overwrites the file with zeros
shred -uvz textfile.txt
By default, shred overwrites the file four times. In the first three passes, it overwrites the file contents with random data. In the last round, because of the -z Flag, it overwrites the data with zeros. Shred also overwrites the inode to remove any metadata associated with the file.
Overwrite a file with zeros
To simply overwrite a file with zeros without deleting it from your system, remove the -u Flag from the previous command.
shred -vz textfile.txt
Set the number of overwrites
As mentioned above, shred overwrites the data in the file four times. If you want to set a specific number of overrides you can do so with the -n or –iterations Flag.
Note, however, that Shred will always add one more pass to the number you specify. To overwrite the file six times, pass the number five in the command:
shred -uvz -n 5 textfile.txt
shred -uvz –iterations 5 textfile.txt
Overwriting the files three times is more than enough to ensure that no one can recover the data. Anything beyond that simply takes more time without having a significant effect.
Delete multiple files with shred
To delete multiple files, just pass the names of the files separated with the place Character.
shred -uvz file1.txt file2.txt file3.txt
If you have a directory that contains similar types of files, you can use wildcard characters like the asterisk (*) to delete or overwrite files. For example, to delete all TXT files in your current working directory:
shred -uvz * .txt
Shred part of the file
You can also use shred to corrupt a file by shredding the first few bytes of a file. For example, you can overwrite or remove the initial 1 KB of the file. In order to do this, -s or –Size Flag is what you need.
While you can view a text file even after partial shredding, package files or executables will not run after running the command.
shred -vz -s 1K textfile.txt
shred -vz –size 1K textfile.txt
The original text file:
Run the command:
The text file after issuing the command:
Shred accepts the following three suffixes in the command:
- K: Kilobytes
- M.: Megabytes
- G: Gigabytes
Get command line help
While the shred command doesn't have many methods and options to remember, there are times when you may want to refer to the command's man page for reference.
The –Help flag displays the shred man page:
chop up – help
Permanently remove files on Linux
Linux gives you an easy way to remove files and folders from your storage. But that's not entirely certain. It only takes a few clicks to access these deleted files with any data recovery software.
If you're using a public computer and don't want anyone to view your files and folders, you can hide them instead. It's a much better way of preventing someone from viewing your personal information on Linux.
How to hide files and folders from prying eyes on Linux
In Linux, if you want to protect sensitive data from other users, learn how to hide files and folders.
About the author
(64 published articles)
Deepesh is Junior Editor for Linux at MUO. He has been writing informational content on the Internet for over 3 years. In his spare time he enjoys writing, listening to music and playing the guitar.
From Deepesh Sharma
Subscribe to our newsletter
Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!
One more step …!
Please confirm your email address in the email we just sent you.