Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Permanently and Securely Delete Files Directories Linux

· 5 min read · Home Tech

In this article
  1. Why Standard Deletion Is Not Secure
  2. How Secure Deletion Works
  3. Using shred for Files
  4. Using wipe for Directories
  5. How to Choose the Right Method
  6. Common Problems and Misconceptions
  7. Alternatives: Whole-Drive Wiping and Physical Destruction
  8. Frequently Asked Questions

To permanently and securely delete files and directories in Linux, you must overwrite the data with random patterns or use a filesystem-level method like encryption or `fstrim`, because the standard `rm` command only removes the directory entry, leaving the data recoverable. The most direct approach is to use a tool like `shred` for individual files or `wipe` for directories, but for modern SSDs, encryption is the only truly secure method.

Why Standard Deletion Is Not Secure

When you delete a file with `rm`, Linux simply unlinks the file's name from the directory structure and marks the disk space as free. The actual data blocks remain on the storage medium until they are overwritten by new data. This means that with the right tools—like `foremost`, `photorec`, or specialized data recovery services—someone can recover your deleted files. This is especially concerning if you are selling a computer, disposing of a drive, or handling sensitive information.

How Secure Deletion Works

Secure deletion involves overwriting the data with new patterns. The idea is that once a data block is overwritten, the original information is gone. Tools like `shred` do this by writing random or patterned data over the file's allocated space. However, this approach has limitations:

  • Filesystem journaling: Journaling filesystems (ext3, ext4, XFS) may keep copies of data in the journal, which `shred` cannot always overwrite.
  • SSD wear leveling: Solid-state drives use wear leveling to distribute writes evenly. This means that the physical location of a file's data may change, and overwriting the logical address may not overwrite the physical cells. The original data may remain in a different physical location.
  • Copy-on-write filesystems: Btrfs and ZFS use copy-on-write, so old data blocks may remain even after overwriting.

For these reasons, secure deletion on SSDs is best achieved through full-disk encryption from the start. If the drive is encrypted, even if a file's data remains, it is unreadable without the encryption key.

Using shred for Files

The `shred` command is included in most Linux distributions (part of the GNU coreutils). It overwrites a file with random data before deleting it. Basic usage:

shred -v -z -n 3 /path/to/file
  • -v : verbose, shows progress
  • -z : add a final overwrite with zeros to hide the shredding
  • -n 3 : overwrite 3 times (default is 3, but you can specify more)

To also remove the file after shredding, add the -u flag:

shred -v -z -n 3 -u /path/to/file

For directories, `shred` cannot delete the directory itself, but you can use it on all files inside:

find /path/to/dir -type f -exec shred -v -z -n 3 -u {} \); then rmdir /path/to/dir

This finds all files, shreds and deletes them, then removes the now-empty directory.

Using wipe for Directories

The `wipe` command is specifically designed to securely erase files and directories. It is not installed by default; you can install it with your package manager (e.g., sudo apt install wipe on Debian/Ubuntu). To delete a directory and all its contents:

wipe -rf /path/to/directory

The -r flag recurses into subdirectories, and -f forces deletion without prompting. `wipe` uses multiple overwriting passes (configurable) and also handles filesystem metadata.

How to Choose the Right Method

Your choice depends on the storage type and your threat model:

  • Hard Disk Drives (HDDs): `shred` or `wipe` are effective because data is written to a known physical location. Overwriting a few times is generally sufficient.
  • Solid-State Drives (SSDs): Overwriting is not reliable due to wear leveling. The best practice is to use encryption and, when disposing of the drive, simply erase the encryption key. Alternatively, use the drive's ATA Secure Erase command (via `hdparm` or `blkdiscard`), which tells the drive to wipe all cells.
  • USB flash drives / SD cards: Similar to SSDs, they use flash memory with wear leveling. Use the device's secure erase if available, or physically destroy the media for high-security needs.

Common Problems and Misconceptions

A common misconception is that overwriting once is enough. While that may be true for modern HDDs, many security standards recommend multiple passes. However, for SSDs, even multiple passes are useless. Another issue is that `shred` may not work on filesystems with journaling or on files that are compressed or sparse. Also, be aware that `shred` cannot overwrite data that is stored in the filesystem's journal or in snapshots (e.g., btrfs snapshots).

Alternatives: Whole-Drive Wiping and Physical Destruction

If you need to securely erase an entire drive, you can use tools like dd to overwrite the whole device:

sudo dd if=/dev/urandom of=/dev/sdX bs=1M status=progress

Replace /dev/sdX with your drive identifier (be very careful!). This writes random data to the entire drive. For SSDs, use blkdiscard to issue a TRIM command that tells the drive to erase all blocks:

sudo blkdiscard /dev/sdX

For maximum security, especially for classified data, physical destruction (shredding the drive or incinerating it) is the only guaranteed method.

Frequently Asked Questions

Is rm -rf secure?

No. rm -rf only removes the directory entries. The data remains on the disk and can be recovered with forensic tools.

Can I recover a file after using shred?

On a traditional HDD, after a few overwrites, recovery is practically impossible. On an SSD, due to wear leveling, some data may remain, so it is not guaranteed.

Does shred work on SSDs?

Not reliably. The SSD's controller may ignore the overwrite commands and place data elsewhere. Use encryption or the drive's secure erase feature instead.

What is the best way to securely delete a file on an SSD?

The most secure method is to use full-disk encryption and then destroy the encryption key. Without the key, the data is unreadable, even if it remains on the drive.

How many overwrites do I need?

For HDDs, one or two passes are generally sufficient for most purposes. The U.S. DoD standard of 3-7 passes is often cited, but modern drives make even one pass effective. For SSDs, overwrites are ineffective, so this question does not apply.

Can I securely delete a single file on a journaling filesystem?

It is difficult because journaling may keep copies of the data. The safest approach is to encrypt the entire filesystem and then delete the encryption key when needed.