Linux Command Reference ยท Files and Directories
Linux shred command
Reference for the Linux shred command: overwrite a file several times, add a final zero pass with -z, remove it with -u, and when shred does not help.
shred overwrites a file with patterns of random data (and optionally zeros) so that its former content is hard to recover from the disk. A normal rm only removes the directory entry; the data blocks stay on disk until something else reuses them. shred writes over those blocks first, and with -u also deletes the file.
It is deliberately blunt: it works on the blocks the file occupies right now. That assumption holds for simple file systems on spinning disks and breaks on most modern storage, which the usage notes below explain. Treat shred as one tool in a plan, not as a guarantee.
shred [OPTION]... FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
shred [-fuvxz] [-n PASSES] [-s SIZE] [--remove[=HOW]] FILE...
| Option | Description |
|---|---|
-n N, --iterations=N |
Overwrite N times (default 3). |
-z, --zero |
Add a final pass of zeros. |
-u |
Truncate and remove the file after overwriting (same as --remove=wipesync). |
--remove=HOW |
unlink, wipe or wipesync (default with -u): how the name is removed; wipesync also renames the file to hide its original name. |
-v, --verbose |
Show progress. |
-x, --exact |
Do not round the size up to a whole block. |
-s SIZE, --size=SIZE |
Shred only this many bytes. |
-f, --force |
Change permissions to allow writing if necessary. |
- as the file name shreds standard output. Device files such as /dev/sdX can be shredded too, which wipes the whole device.
Returns: 0 on success; 1 if any file could not be shredded.
Usage notes
- Journaling and copy-on-write file systems (ext3/4 in data-journal mode, Btrfs, ZFS, XFS with reflinks, NFS and many network file systems) may write new blocks instead of overwriting old ones, or keep copies in a journal or snapshot.
shredcannot reach those copies. - SSDs and flash remap blocks through wear levelling, so the old data can survive in blocks the operating system cannot address. For SSDs use the drive's secure-erase or full-disk encryption instead.
- Backups, snapshots, caches and swap may hold other copies. Shredding the file does not touch them.
- Encryption is the better plan: keep sensitive data on an encrypted volume and destroy the key.
- Size rounding: by default the size is rounded up to the next full block (so a 3-byte file becomes one block);
-xkeeps the real size. - Hard links:
shred -uremoves only the name you gave; other links still point at the overwritten data. - Speed: three passes over a large file are slow. One random pass is usually plenty for modern magnetic media; the old 35-pass advice dates from very old drives.
- Whole devices:
shred -v -n 1 /dev/sdXwipes a disk. Triple-check the name, and do not do it on a mounted disk. Seeddfor the same danger.
Related
rm,dd,truncate,chattr and lsattr- Lessons: Hardening, Copy, move and remove