Shred and DD Commands: 5 Easy Tips, Avoid Wipe Mistakes

Shred and dd are the two classic Linux tools for overwriting data, and each has traps that the old version of this post did not mention. This rewrite runs both on coreutils 9.7 in a Debian 13 container, on ordinary files and on a 16 MiB image file that plays the part of a disk. No real device was touched. The tests contradict four statements of the old text: shred does not truncate the file, find ... -exec shred fails on directories, conv=notrunc does not do what the text says, and wipefs is not a wiping tool.

Scope: all targets were files inside a container. Block devices, SSDs and hardware wipe commands were not tested.

Shred and dd: what shred does to one file

The basic call is shred file. The old text says it overwrites the file three times and then truncates it to zero length. The first part is right: shred -v printed pass 1/3 (random)..., pass 2/3 (random)... and pass 3/3 (random).... The second part is wrong. A 2100-byte file with a repeated secret line had 4096 bytes after shred, because shred rounds the size up to a whole block, and the content was random bytes: grep -c SECRET found 0 matches. The file stays where it is. To remove it, use -u.

  • shred -n 7 -vz file ran 8 passes: random, 555555, ffffff, random, aaaaaa, 000000, random, and a final 000000 pass from -z. The file then showed only zeros in xxd and kept the 4096-byte size, so an observer cannot tell it held data.
  • shred -u -v file did three passes, then renamed the file step by step (000000, 00000, down to 0) and removed it: ls reported that the file no longer exists. The renaming hides the original name.

The old text also quotes a DoD standard for seven passes. That was not verified, and the manual of shred does not require it; read the shred section of the coreutils manual instead. The same manual text appears in shred --help: "shred assumes the file system and hardware overwrite data in place. Although this is common, many platforms operate otherwise." Journaling or copy-on-write file systems, backups and SSD wear leveling are the cases where it can fail.

Shred and dd test: shred rounds the file to 4096 bytes, find with shred fails on directories, conv=notrunc keeps the size, wipefs erases only 2 bytes
Four results for shred and dd that differ from the old text.

Shred a directory the right way with shred and dd tools

The old command was find /path -depth -exec shred -n 7 -u {} \;. On a folder with two files it printed shred: d/sub: failed to open for writing: Is a directory and the same for d. The files were shredded, but both directories stayed, and find still exited with 0, so a script would think all was fine. Add -type f and remove the empty directories afterwards:

find d -type f -exec shred -n 3 -u {} \;
find d -depth -type d -empty -delete

After this, ls -d d answered that the directory does not exist.

Shred and dd: overwriting a disk with dd

On the 16 MiB image, dd if=/dev/urandom of=disk.img bs=4M count=4 status=progress conv=notrunc replaced the pattern completely: before, grep -c DATA found 1290555 lines; after, grep -c DATA-ON-DISK found 0, and the size stayed 16777216 bytes. Writing zeros with /dev/zero worked the same way. To check, cmp disk.img /dev/zero printed cmp: EOF on disk.img after byte 16777216 with exit code 1: no difference was found, cmp only reports that the image ended before the infinite zero stream. Read that message as "all zero".

Two warnings come from the lab. First, a regular file has no end for dd: the first run of this test left out count=4, and dd wrote about 45 GB into the image before it was stopped. A real device ends at its size and stops by itself, but check the target name twice, because dd never asks.

Second, the old text says that conv=notrunc "ensures that the entire device is overwritten". The test shows what it really does: writing 2 MiB over an 8 MiB file without it left a 2097152-byte file, with it the file stayed 8388608 bytes. The option only matters for regular files; for a block device the test was not run.

wipefs does not wipe data like shred and dd

The FAQ of the old post lists wipefs as an alternative. After mkfs.ext4 on the image, wipefs -a disk.img reported 2 bytes were erased at offset 0x00000438 (ext4): 53 ef. That is the file system signature and nothing more. It prevents the system from recognising the file system; it does not remove the data.

A safe routine for shred and dd

Follow the same routine every time you use shred and dd, and the lab shows why each step is there. First, identify the target and say its name aloud: a file path for shred, a device or image for dd. Second, run the command on a copy or an image file with count set, and compare the result with grep or cmp. Third, for files use shred -u -v so you see the passes and the removal. Fourth, for devices use dd with status=progress and check the target again with lsblk just before you press Enter.

Fifth, verify the result: read some blocks back and look for the old pattern. The point of shred and dd is that nothing asks you to confirm, so the routine has to do it.

Common mistakes with shred and dd

  • Expecting shred to truncate or delete the file without -u.
  • Using find without -type f and trusting its exit code.
  • Running dd on a file without count, or on the wrong device.
  • Counting on shred for SSDs, snapshots or copy-on-write file systems.
  • Using wipefs as a security tool.

If you wrap these commands in a script, test the redirections and quoting first; the mistakes are the same ones listed in the bash script examples post.

FAQ

How many passes do I need with shred and dd? The lab only compares results: one random pass already removed the test pattern from the image. The old DoD figure was not verified.

Can I use shred and dd on an SSD? The manual quoted above warns about hardware that does not overwrite in place. SSDs were not tested here, so use the maker's secure erase function.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance