Learn how to permanently delete files on a Linux system using the command line and GUI
For users of any operating system on any device, deleting files is a fairly common task. Whether you want to delete unused files on your PC, or you want to free up space by deleting older log files on the server, it is wise to understand the various options for file deletion.
When it comes to data privacy and the various legalities that surround it, secure file deletion is also an important measure. Many new file systems use new technologies such as Journaling, where deletion does not “delete” the data, but instead creates a “deleted” entry for the deleted file in the registry and marks its space as available. A simple
rm command cannot guarantee that “deleted” content cannot be restored at all.
From the rm man page:
If you use rm to delete files, if you have enough experience and/or time, you may be able to recover some of them.
Therefore, it is important to understand the tools in Linux. These tools at least to a certain extent guarantee that the data is deleted and cannot be recovered using recovery tools or any other data recovery methods.
Command Line Tools to Permanently Delete Files on Linux
rm is the standard program to remove files in GNU/Linux systems. It is a part of GNU Coreutils and comes pre-installed in almost all Linux distributions.
To delete file(s) using rm, you can run:
rm file1 file2 /home/user/file3
This does not work on directories. To delete entire directories, along with the hierarchies below, you can run:
rm -r dir1 /home/user/dir2 file3
Data deleted using
rm is recoverable until new data is written on the disk space occupied by the deleted data. Hence,
rm is a good option if the data to be deleted does not contain any sensitive information.
The shred command overwrites the file with random data multiple times along with the option to delete the file. This makes recovery of the data extremely improbable, even with expensive hardware.
To shred contents of file (overwrite with random data), run the following command:
Note that by default, it overwrites random data 3 times. To overwrite in a different number of iterations, run the following command:
shred -n 10 filename
This will overwrite the data 10 times. Note that the above will not delete the file, only data is overwritten.
shred to delete and overwrite the contents of a file, use the following command:
shred -n 10 --remove filename
One downside with using shred is absence of a ‘recursively shred’ option.
srm is part of the secure-delete package in Debian and Red Hat-based distributions. It uses a similar method as
shred for secure deletion of a file. However, the algorithm used for overwriting a file are different in both the tools.
srm on Ubuntu and similar distributions, run the following command:
sudo apt install secure-delete
Note: For older Ubuntu versions (version 14.04 and below), you need to use
apt-get should be used instead of
srm on Red Hat based distributions, run the following command:
yum install secure-delete
To delete your files and folders recursively using
srm, run the follwing command:
srm -r foldername/
GUI tools to Completely Delete Files on Linux
Nautilus is the default file explorer for most Linux distributions. You can permanently delete files in Nautilus by following the instructions below.
First, open Nautilus and Go to the folder from which you wish to delete the files.
Select the file/folder and press key combination
Shift + Delete.
On the confirmation dialogue, click Delete to permanently delete the file or folder.
In case you prefer using mouse over keyboard, then you can add the Delete option in the context menu so that you can right-click on files/folders and select Delete. By default, the only option context menu has is “Move to trash”.
To enable the option of permanent delete in the right-click menu, do the following:
- Go to Edit » Preferences in the file explorer.
- Then select the Behaviour Tab.
- Check the box for Include a Delete command that bypasses Trash.
This will add a Delete option to the context menu in Nautilus on Ubuntu and other Linux distros.
Using Nautilus Scripts (For running any program from GUI)
Nautilus has the option of adding manual scripts to execute on selected files. We can make use of this to run
srm command from GUI.
Let us create a script to run
srm recursively. Open the terminal, and go to Nautilus scripts folder location by running the command below:
Create a blank script file using the command below:
Add the following lines to the script file that we created in the step above.
#!/bin/bash srm -r $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is a variable that contains paths of all files and folders selected by the user in Nautilus.
Save the file by first pressing the
ESC key, and then type
:wq to save the file and exit the vim console.
Finally, make the script executable by granting execute permission using the command below.
chmod +x Secure_Delete
After setting the script file, go back to the Nautilus GUI and right-click on a file or folder. You should see the script Secure_Delete under Scripts option in the context menu.
Click on the script name (that is Secure_Delete in this case) to permanently delete the files you selected before right-clicking.
In a similar way, you can add script for
shred or any other tool and execute it from the GUI.
There are more GUI tools available, such as Nautilus-wipe and Bleachbit, which also use similar algorithms as
srm. Both can be installed from the standard Ubuntu repository.
Note that even after using these methods, there still lies a small chance that the data can be recovered using software (Disk recovery) or hardware methods (Hard Disk Drive Freezing). Hence in case of extremely sensitive data to be deleted permanently, methods like heating the hard drive at 1500 degrees Celsius make sure that no tools can recover any data from the disk.
Related: What’s the Difference Between Bash, Zsh, and Other Linux Shells?