Categories
Apps 💡 Linux

How to Permanently Delete Files in Linux using Command Line and GUI Tools

The deletion of files is quite a prevalent task for users of any operating system on any device. Whether you want to delete unused files on your PC, or you want to free up space by deleting older log files on your server, it is handy to know various options for file deletion.

Secure deletion of files is also a vital measure when it comes to data privacy and various legalities surrounding it. Many new file systems use new technologies such as Journaling, in which deletion does not “delete” the data, but makes a “Deleted” entry for the deleted file in the Journal, and marks its space as available for use. A simple rm command does not, at all, guarantee that the “deleted” contents cannot be recovered.

From the rm man page:
If you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time.

Hence, it is essential to know tools in Linux which guarantee, at least to a degree, that the data is deleted and cannot be recovered by either a recovery tool or any other method for data recovery.

Command Line Tools to Permanently Delete Files on Linux

Using rm command

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.

Using shred command

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:

shred filename

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.

To use 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.

Using srm command

The program 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.

To install 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 apt.

To install 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

Using Nautilus

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 shred or 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:

cd ~/.local/share/nautilus/scripts/

Create a blank script file using the command below:

vim ~/.local/share/nautilus/scripts/Secure_Delete

Add the following lines to the script file that we created in the step above.

#!/bin/bash
srm -r $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS

Here $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 shred and 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.