How to Securely Delete Files in Linux
Deleting a file in Linux doesn’t remove the data from your hard drive. Learn how to securely delete files in Linux so they’re unrecoverable.
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
- Press Ctrl + Alt + T to open a Terminal window.
- 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
- Press Enter.
- Type your account password and press Enter.
- 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.
- Press Ctrl + Alt + T to open a Terminal window.
- 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
- 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.
- Press Ctrl + Alt + T to open a Terminal window.
- Type the following command:
whereis shred
- Press Enter.
How to Securely Delete a File Using Shred
- Press Ctrl + Alt + T to open a Terminal window.
- 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
- The -u option tells shred to delete the file after overwriting it. To only overwrite a file without deleting it, leave out the -uNote: 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.
- 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
- Press Enter.
Each pass over the file as it’s overwritten is listed as it happens.
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
- Press Ctrl + Alt + T to open a Terminal window.
- 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
- Press Enter.
- Type your account password and press Enter.
- 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
- Press Ctrl + Alt + T to open a Terminal window.
- 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.
wipe /home/lori/Documents/delete-this-file.txt
- 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
- 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.