Why the ‘rm’ Command is Not Secure
When you delete a file in Linux using the standard rm (remove) command, or by moving it to the trash via the graphical interface, the operating system does not actually destroy the file’s contents.
Instead, Linux simply removes the "pointer" that tells the file system where the data is located, marking that space on the hard drive as available for new data. Until the system eventually overwrites that specific sector of the disk with a new file, the original data remains completely intact. Anyone with basic data recovery software can easily undelete the file and read its contents.
If you are dealing with sensitive data—such as financial records, private encryption keys, or password databases—this standard deletion method is a massive security risk. To permanently destroy a file so that it cannot be recovered, you must forcefully overwrite the data before deleting the pointer. In Linux, the tool for this job is the shred command.
How the Shred Command Works
The shred command takes a file and forcefully overwrites its contents with random data (like zeros and ones) multiple times. Even if someone attempts to recover the file later using advanced forensics, all they will find is the random garbage data that shred left behind.
Warning: The shred command is permanent and irreversible. Once you execute this command, the data is gone forever. Double-check your file paths before pressing Enter.
How to Use the Shred Command
By default, the shred command overwrites the file three times. However, by default, it does not actually delete the file structure itself—it just scrambles the contents inside the file. To overwrite the contents and then delete the file entirely, you need to use specific "flags" (options).
- Open your Linux terminal.
- To securely wipe a file and delete it, type the following command, replacing
filename.txtwith the actual path to your file:shred -u filename.txt - Press Enter.
The -u flag stands for "unlink" (delete). The command will quietly run in the background. It may take a few seconds for large files, as writing random data three times takes longer than a standard deletion.
Adding Verbose Output and Extra Passes
If you are wiping a very large file, sitting at a silent terminal can be unnerving. You can use the -v (verbose) flag to force shred to print its progress to the screen.
Furthermore, if you are paranoid about data recovery, you can use the -n flag followed by a number to specify exactly how many times the file should be overwritten. The default is 3, but you can increase it.
To overwrite a file 5 times, show the progress, and delete the file, use this combined command:
shred -v -n 5 -u filename.txt
The terminal will output something like this:
shred: filename.txt: pass 1/5 (random)...
shred: filename.txt: pass 2/5 (ffffff)...
shred: filename.txt: pass 3/5 (random)...
shred: filename.txt: pass 4/5 (000000)...
shred: filename.txt: pass 5/5 (random)...
shred: filename.txt: removing
shred: filename.txt: renamed to 000000000000
shred: filename.txt: removed
Notice that shred even renames the file to a string of zeros before deleting it, ensuring that not even the original filename can be recovered from the disk index.
Important Limitations on SSDs
It is crucial to understand that shred was designed for traditional, spinning magnetic Hard Disk Drives (HDDs).
Modern Solid State Drives (SSDs) use a technology called "wear leveling" to prolong the life of the flash memory chips. When you tell an SSD to overwrite a file, the drive’s firmware often intercepts the command, writes the new random data to a completely different physical location on the chip, and marks the original location as ready for garbage collection.
This means that on a modern SSD, shred might not actually overwrite the original physical data blocks immediately. While it is still much safer than a standard rm deletion, the only truly guaranteed way to secure data on a modern SSD is to use Full Disk Encryption (like LUKS) from the moment the operating system is installed.