How-To

How to Securely Delete Files in Linux

Delete on a keyboard

Deleting a file in Linux doesn’t completely remove the data from your hard drive. Learn how to securely delete files in Linux so they can’t be recovered.

When you delete a file on your computer, the file is removed by the system, but it isn’t permanently deleted. You can usually recover deleted files using file recovery tools.

If you have files containing sensitive information, you can delete these files using a more secure method so they can’t be recovered and viewed. We’ll show you a few ways to do this in Linux.

If you want to know how to securely delete files in Linux, follow our guide below.

Should You Use Secure Deletion Tools on an SSD?

The tools we discuss here are for use only with regular spinning hard drives (HDDs).

To be clear—do not use secure deletion tools on solid state drives (SSDs). You can only write files to an SSD a finite number of times. By using secure deletion tools, you’re adding to the  amount of data being written to it, which can cause an SSD to fail much sooner.

If you want to securely wipe files from an SSD, check your manufacturer’s user manual first. It may recommend specific tools for the task. Another alternative is to consider encrypting your hard drive. This will allow you to delete files normally while maintaining a high level of data security.

How to Securely Delete Files in Linux Using secure-delete

Secure-Delete is a set of tools for Linux that includes a tool to securely delete files by overwriting the hard disk space containing the files during deletion.

There are four tools in the set. We’re going to use the srm tool. The other three tools allow you to overwrite the free space on the disk (sfill), overwrite swap space (sswap), and wipe the RAM (sdmem).

How to Install secure-delete

To install secure-delete in Ubuntu, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. This command works on Ubuntu and other Debian-based Linux distributions, like Linux Mint and PureOS.
    sudo apt-get install secure-delete

    Install secure-delete in Linux

  3. Press Enter.
  4. Type your account password and press Enter.
  5. If you’re using a Red Hat-based Linux distribution, like Fedora or CentOS, use the following command:
    sudo yum install secure-delete

How to Securely Delete a File Using the srm Command

The srm command works similarly to the rm command, which removes and deletes a file. However, the srm command overwrites the file multiple times with random data before deleting the file.

To securely delete a file using the srm command, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to delete.
    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
    sudo srm /home/lori/Documents/delete-this-file.txt

    Securely delete a file using secure-delete in Linux

  3. There is no confirmation when deleting files using the srm command, so be sure you want to delete the file before running the command.

How to Securely Delete Files in Linux Using shred

Shred is another tool for securely deleting files. It overwrites the contents of a file multiple times with randomly generated data to make it unrecoverable. Then, you can choose to delete the file.

How to Check if Shred is Installed

Shred is installed by default on all Linux distributions. You can check and find its installation path.

To check if shred is installed:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command:
    whereis shred

    Run the whereis shred command in Linux

  3. Press Enter.

How to Securely Delete a File Using Shred

To securely delete a file using shred, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to delete.
    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
    shred -u /home/lori/Documents/delete-this-file.txt
  3. The -u option tells shred to delete the file after overwriting it. To only overwrite a file without deleting it, leave out the -u
    NOTE: By default, shred overwrites a file with random data 25 times. If you want it to overwrite a file more than this, specify the desired number using the -n option. For example:

    shred -n 50 filename

    The larger a file is, the longer it will take to securely delete it. Keep this in mind when deciding how many times to overwrite the file.

    Securely delete a file using the shred command in Linux
  4. You can also use the -v option (verbose) to display extended information about the overwrite process as it’s occurring. To use the verbose option, type the following command. Again, replace the path in the command, with the path to the file you want to delete.
    shred -v -u /home/lori/Documents/delete-this-file.txt
  5. Press Enter.
    Each pass over the file as it’s overwritten is listed as it happens.
    Run shred in verbose mode in Linux

How to Securely Delete Files in Linux Using Wipe

The wipe command-line utility writes special patterns 34 times to files, eight of which are random. This is the most thorough method of securely deleting files, but it can also take a long time.

How to Install Wipe

To install wipe, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. This command works on Ubuntu and other Debian-based Linux distributions, like Linux Mint and PureOS.
    sudo apt-get install wipe

    Install wipe in Linux

  3. Press Enter.
  4. Type your account password and press Enter.
  5. If you’re using a Red Hat-based Linux distribution, like Fedora or CentOS, use the following command:
    sudo yum install wipe

How to Securely Delete a File using wipe

To securely delete a file using wipe, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command with the path to the file you want to delete.
  3. NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
    wipe /home/lori/Documents/delete-this-file.txt

    Securely delete a file using wipe in Linux

  4. If you are deleting a big file, you can use the quick mode (-q option), which overwrites the file four times by default. To use quick mode, type the following command:
    wipe -q /home/lori/Documents/delete-this-file.txt

    Securely delete a file using wipe with Quick Mode in Linux

  5. You can also specify the number of passes for a quick wipe, in case you want something between four and 34. Instead of -q in the above command, use -Q.

Protect Your Private and Sensitive Data

The steps above give you plenty of options to get started if you’re unsure how to securely delete files in Linux. You can also securely delete files on Mac and on Windows.

You should consider protecting your data even further by using strong passwords and checking the strength of your current passwords. We recommend using a password manager like LastPass or 1Password to help store your passwords and other sensitive information.

Click to comment

Leave a Reply

Your email address will not be published.

 

To Top