How to Securely Delete Files in Ubuntu?

When you delete a file in Ubuntu using the standard “Trash” or rm command, the data isn’t actually erased from your storage drive; instead, the system simply marks the space as available for overwriting. This comprehensive guide explores how to permanently and securely delete files in Ubuntu using powerful command-line tools like shred, srm (secure-delete), and wipe. By following these methods, you can ensure that sensitive data is completely unrecoverable, even by advanced forensic software.


Understanding Why Standard Deletion Fails

When a file is deleted normally, the operating system removes the pointer to the file, leaving the actual binary data intact on your Hard Disk Drive (HDD) or Solid State Drive (SSD). Until new data overwrites that specific sector, data recovery tools can easily piece the file back together. Secure deletion tools prevent this by overwriting the file’s sectors with random data or zeros multiple times before removing the file pointer.


Method 1: Using the Native shred Command

Ubuntu comes with a built-in utility called shred that overwrites files to hide their contents and optionally deletes them. It is highly effective for traditional HDDs.

To securely overwrite and delete a file, use the following command structure:

shred -u -v -n 3 secret_file.txt

Method 2: Using the secure-delete Toolset

For a more advanced and thorough sanitization process, the secure-delete package provides specialized tools that handle files, directories, and even swap space.

First, install the package via the terminal:

sudo apt update
sudo apt install secure-delete

Once installed, you can use the srm (secure remove) command, which fills the file space with 38 passes of specific patterns designed to wipe data completely:

srm -v secret_file.txt

If you need to delete an entire directory and all of its contents securely, add the recursive flag:

srm -r -v /path/to/sensitive_directory

Method 3: Using the wipe Utility

Another robust alternative available in the Ubuntu repositories is wipe. It was specifically designed to securely erase files from magnetic media.

Install the utility using:

sudo apt install wipe

To securely delete a specific file, run:

wipe secret_file.txt

For directories, you must include the recursive (-r) flag along with the force (-f) flag to confirm the operation:

wipe -rf /path/to/sensitive_directory

A Critical Note on SSDs and NVMe Drives

It is important to understand that modern Solid State Drives (SSDs) and NVMe drives utilize a technology called Wear Leveling. This architecture distributes write operations evenly across the drive to extend its lifespan, meaning utilities like shred, srm, and wipe might write to a new sector instead of the exact sector holding the old data.

For absolute security on an SSD, encrypting your entire drive during the Ubuntu installation process is the most effective defense. Alternatively, you can use drive-specific manufacturer tools to execute an “ATA Secure Erase” or “NVMe Format” command, which clears all blocks at the hardware controller level.