Smart Systems for Home

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

Completely Delete File in Linux: 6 Methods

· 6 min read · Home Tech

In this article
  1. Why rm Isn't Enough
  2. How Secure Deletion Works
  3. Method 1: Using shred (GNU coreutils)
  4. Method 2: Using wipe
  5. Method 3: Using dd for a Quick Overwrite
  6. Method 4: Using srm (Secure Remove)
  7. Method 5: Using a Script for Multiple Files
  8. Method 6: Encrypt the File and Delete the Key
  9. Common Problems and Limitations
  10. How to Choose the Right Method
  11. Frequently Asked Questions

To completely delete a file in Linux, you must overwrite its data before unlinking it, because a standard rm only removes the directory entry, leaving the data recoverable on the disk. This article explains why simple deletion is not enough and shows you several reliable methods to ensure the file's contents are truly gone.

Why rm Isn't Enough

When you run rm file.txt, the kernel removes the file's name from its directory and marks the disk blocks as free. The actual bytes remain on the storage medium until another process overwrites them. With common tools like foremost or photorec, anyone with access to the disk can recover that data, even after a format. For sensitive documents, passwords, or private keys, this is a serious security risk.

To truly delete a file, you must overwrite its content with other data (zeros, random bits, etc.) before unlinking. This is called secure deletion or shredding. The process is straightforward but requires the right tool and an understanding of filesystem behavior.

How Secure Deletion Works

Secure deletion tools write patterns over the file's allocated blocks. The most common approach is to overwrite the file multiple times with different patterns, as recommended by the U.S. Department of Defense (DoD) standard 5220.22-M, which specifies three passes: all zeros, all ones, and a random pattern. However, for modern hard drives, a single pass of random data is often considered sufficient because of the high density of storage.

Important: This only works if the filesystem actually writes to the same physical sectors. On journaling filesystems (like ext3, ext4, XFS) or on SSDs with wear leveling, the overwrite may not hit the original location, leaving remnants in the journal or in a different flash block. For SSDs, the only reliable way to erase a single file is to use the drive's built-in secure erase command (via hdparm or the manufacturer's tool), which clears the entire disk, not just one file.

Method 1: Using shred (GNU coreutils)

The most common tool is shred, which is preinstalled on most Linux distributions. It overwrites the file with random data by default.

  1. Open a terminal.
  2. Run: shred -v -z file.txt
    • -v shows progress.
    • -z adds a final overwrite with zeros to hide the shredding.
  3. After shredding, remove the file with rm file.txt.

To shred and remove in one step, use shred -u file.txt. The -u flag unlinks the file after overwriting.

Example: shred -v -z -u secret.doc

Method 2: Using wipe

wipe is a dedicated secure-deletion tool that performs multiple passes with configurable patterns. It is not installed by default, so you may need to install it via your package manager (e.g., sudo apt install wipe on Debian/Ubuntu).

Usage: wipe -f file.txt – the -f forces deletion. It will overwrite the file 34 times by default, which is overkill for most situations but provides maximum assurance.

Method 3: Using dd for a Quick Overwrite

If you want a simple single-pass overwrite without installing extra tools, you can use dd to fill the file with zeros:

dd if=/dev/zero of=file.txt bs=1M conv=notrunc

This writes zeros over the file's content. Then remove it with rm. Note that conv=notrunc prevents truncating the file before writing.

Method 4: Using srm (Secure Remove)

srm is another secure deletion utility, often part of the secure-delete package. It overwrites the file with random data, then deletes it. Installation: sudo apt install secure-delete.

Usage: srm -v file.txt

Method 5: Using a Script for Multiple Files

If you need to securely delete many files, you can combine tools with a loop:

for f in *.txt; do shred -u "$f"; done

This will shred and remove all .txt files in the current directory.

Method 6: Encrypt the File and Delete the Key

An alternative approach is to encrypt the file with a strong passphrase, then delete the encrypted version and forget the passphrase. Without the key, the data is effectively unrecoverable. Tools like gpg or openssl can do this. While not a deletion method per se, it renders the data unreadable, which is often the goal.

Common Problems and Limitations

  • SSD and flash drives: Overwriting may not reach the original cells due to wear leveling. The only secure option is full-disk encryption (use it from the start) or the drive's secure erase command.
  • Journaling filesystems: The file content may be duplicated in the journal, so even after shredding, a copy might exist. You can disable journaling for a specific partition (not recommended) or accept the risk.
  • Copy-on-write filesystems (Btrfs, ZFS): Old data blocks may remain after overwriting because the filesystem writes to a new location. The original block is only freed later, and may be recoverable.
  • Swap space: If the file was ever swapped to disk, its content may be in the swap partition. Use swapoff and swapon to clear swap, or better, use full-disk encryption.
  • Backups: If the file was backed up, no local deletion will remove the backup copy. You must delete the backup as well.

How to Choose the Right Method

Consider the following factors:

  • Type of storage: For HDDs, shredding works. For SSDs, use encryption or the drive's secure erase.
  • Filesystem: On ext4, shredding is generally effective for the file's data blocks, but not for the journal. For maximum security, use full-disk encryption.
  • Urgency: If you need a quick solution, dd with zeros is fast. If you need maximum assurance, use wipe with many passes.
  • Number of files: For many files, a script with shred -u is efficient.

Frequently Asked Questions

Does rm ever overwrite data?

No. rm only unlinks the file. The data remains on disk until overwritten by other operations. This is why forensic tools can recover deleted files.

Can I recover a file after using shred?

On a traditional HDD, it is extremely unlikely to recover data after a proper shred with multiple passes. However, on SSDs or with journaling filesystems, remnants may exist. For absolute security, use encryption.

Is one pass enough?

For modern hard drives, a single pass of random data is generally considered sufficient because of the high data density. Multiple passes are a legacy from older drives with lower density. However, some organizations still mandate three passes (DoD standard).

What about the trash command?

The trash command (or moving to a trash directory) does not delete the file at all; it just moves it to a hidden folder. You still need to empty the trash, and even then, the data remains recoverable.

Does secure deletion work on USB flash drives?

USB flash drives are SSDs in essence, so they have the same wear-leveling issue. Overwriting may not hit the original cells. The only reliable method is to use the drive's secure erase feature, if available, or to encrypt the data from the start.

Why is my shred command failing with "Operation not permitted"?

This often happens when the filesystem does not support the operation (e.g., on some network mounts) or when the file is on a read-only filesystem. Check the mount options and permissions. Also, some filesystems (like FAT) may not support all features.