As you all know, with Linux there are numerous ways to deal with each and every task. Some of these ways are obvious, some are subtle, and some are as brute-force as an NFL offensive line. Each of these methods will do the same thing but, in some cases, the results are a bit different.
Such is the case with removing files. There are two fundamental ways to remove a file:
With the help of a GUI such as Thunar, Dolphin, or Nautilus
With the help of a command such as rm or shred.
In this article we will primarily examine the latter and touch on the former.
The former - GUI
As you would expect, in all three of the graphical file managers you can easily remove a file simply by right clicking the file and selecting Delete. There are, however, some subtle differences. You will notice, in both Dolphin and Nautilus you can choose between Deleting a file and moving a file to the Trash. This behavior mimics that of Windows Explorer. Once you move a file to the Trash it will remain there until you empty your trash. Should you select to actually Delete the file, however, that file is deleted right away.
This behavior is not available in Thunar. With Thunar you can keep it or delete it.
rm that file
The basic remove command is rm. It is used like this:
Believe it or not there are options to go along with the rm command. There are two options that are used most often:
f - This option will force the delete. In other words you will not have to answer "y" to every file you want to delete. This is very handy when you are deleted a directory.
r - This option will recursively delete all files within a directory as well as the directory itself.
As you can surmise, the r and f options are most often used together. So to delete the ~/TEST directory and all of its files you would issue the command:
rm -rf ~/TEST
If you only wanted to delete the file test.txt within the ~/TEST directory you would issue the command:
What if you wanted to delete all jpg files within the ~/TEST directory? Simple:
rm -f ~/TEST/*.jpg
The * character is a wild card which means, in this case, anything that ends with .jpg. Notice the ".". If you leave that out you can delete any file ending in jpg - not just any file with the extension .jpg.
shredd'ing a file
The shred command is a whole different beast. Shred is for the paranoid. Shred completely obliviates a file by overwriting its contents. Instead of deleting a file shred will simply overwrite a file and the end results will be an unreadable binary file that can then be safely removed.
And no matter how paranoid you are, shred will help you out. You can declare how many overwrite iterations shred will perform. You can even add a final overwrite of nothing but zeros to hide the fact that you ever shredded a file. And finally, you can also add a switch to inform shred to automatically remove the file after the iterations.
Shred is used like so:
shred OPTIONS filename
The most popular options are:
v - Show progress.
z - Add final overwrite of zeros.
u - Remove file after iterations
n - Overwrites n times
So to shred a file by iterating 10 times, followed by a final iteration of zeros, and then deleting the file upon completion you would issue the command:
shred -v -n 10 -z FILENAME -u
Where FILENAME is the actual file name.
After that command your removed file has vanished into thin air.
Many different routes to the same destination. Of this, Linux is the master. In the case of removing files, you can send it to the trash for possible later easy retrieval, remove it with the possibility of a much more difficult retrieval, or shred it with no hopes of retrieval. Your call.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.