Command - Securely Erase Files and Disks with shred
When you delete a file normally, the data is often not truly gone right away. The filesystem usually just marks the space as available, which means recovery tools may still be able to find old content. That matters when you are retiring a USB drive, removing logs with sensitive data, or cleaning up files that contain secrets, credentials, or private information.
One simple Unix tool for this job is shred. It repeatedly overwrites data before removing it, making casual recovery much harder.
What shred Does
shred is a command-line tool that overwrites files or devices with random-looking data and then optionally deletes them.
Important options:
-
-u
Remove the file after overwriting it. Without this, the file remains on disk. -
-n N
Number of overwrite passes. More passes take longer. -
-z
Add a final pass of zeros to hide the fact that shredding took place. -
-v
Verbose output, useful for tracking progress. -
-f
Force permission changes if needed.
A few practical notes:
shredis best for regular files and some removable media.- It is less reliable on SSDs (solid-state drives) because wear leveling can move data around internally.
- It is not a guarantee against advanced forensic recovery in all cases.
- For full-disk cleanup, consider using it only when you understand the storage type and risks.
Practical Examples
Securely erase a single file
shred -u -v secret-notes.txtThis overwrites the file and then deletes it.
Use multiple overwrite passes
shred -u -v -n 3 secret-notes.txtThis performs three overwrite passes before removal.
Add a final zero pass
shred -u -v -n 3 -z secret-notes.txtThis makes the file appear like ordinary empty data after shredding.
Wipe a removable disk or partition
First, identify the device carefully:
lsblkThen shred the device by name, for example:
sudo shred -v -n 1 -z /dev/sdbIf you want to remove the partition table and contents from a removable drive, this will overwrite the entire device. Be extremely careful: choosing the wrong device can destroy the wrong disk.
Safer approach for a mounted file system
Unmount the device first:
sudo umount /dev/sdb1Then shred the partition:
sudo shred -v -n 1 -z /dev/sdb1Requirements
- GNU coreutils package, which includes
shredon most Linux systems sudoprivileges for raw disks and partitions- A Unix-like system such as Linux, BSD, or macOS with compatible tooling
Final Notes
Use shred when you need a simple overwrite-based cleanup tool for files or removable media. For modern SSDs, encrypted drives, or enterprise storage, device-specific secure erase methods may be more appropriate. Always verify the target device before running a destructive command.