Get to know Linux: Removing files - gHacks Tech News

Get to know Linux: Removing files

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:

rm OPTIONS

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:

rm ~/TEST/test.txt

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.

Final thoughts

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.





  • We need your help

    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 (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Tom said on May 20, 2009 at 12:57 pm
      Reply

      Hi,

      if I understand you correctly, you are saying that Thunar does not have a Trash, right?

      But this is not the case for Xubuntu. Here the Thrash works like under Windows.

      Tom

    2. G David Lewis said on May 20, 2009 at 1:33 pm
      Reply

      One of the best things about Linux is the ability to quickly and easily throw frequently and commonly used commands along with any options into a quick script. For instance, with the shred command above, one could use this simple little script…

      #!/bin/bash

      shred -v -n 10 -z [email protected] -u

      exit

      …where the [email protected] would represent any file(s) passed to the script on the commandline. Throw this into a text file, name it something like “obliterate’, make it executable and place it somewhere in your path or even on your desktop. Then instead of typing all that, you can simply type “obliterate filename(s)”, or drag and drop the file(s) onto the icon for the script and poof – they will be gone!

      Such is the power of Linux, it’s a beautiful thing!

    3. jack said on May 20, 2009 at 1:47 pm
      Reply

      looks like that just made it into the 1.0.0 release a couple of months ago. believe it or not all of my debian installs have 0.9.0 on them which does not have this feature.

      thanks for pointing this out!

    Leave a Reply