Aug 312013

Every Linux/Unix user ought to be familiar with the old rm -Rf * gag. Or the more subtle rm -Rf files/ * issue, in which a misplaced space results in the removal of all files and directories. An administrator is going to use the rm utility a hundred times a day. How can they remain efficient while insuring a simple mistake doesn’t result in downtime and serious data loss?

Some Linux distributions ship with interactive mode enabled by default. This means the rm utility asks before removing each file. From what I’ve seen, admins typically disable or override this, as it’s just too unwieldy for practical use. But this means they may not realize what they’re removing until after the command has started deleting things.

Without a doubt, backups and snapshots offer some additional safety, but it takes time to recover. It’s better to know exactly what you’ll be removing rather than double-checking your wildcards and assuming you typed it correctly.

Tab Completion

This doesn’t always work (depending upon your shell settings), but allows you to view what bash intends to remove by double-tapping the TAB key after your asterisk. It works much like typical tab-completion for commands, files and directories:

eliot@penguin:~$ rm * <TAB><TAB>
eliot@penguin:~$ rm *

Live Expansion

Bash also allows for glob expansion as you’re typing. Think of this as a more sophisticated tab-completion capability. Instead of printing the list of files which will be removed, your asterisk character is replaced by the names of all the files selected by the wildcard. The expansion is triggered by typing CTRL+x and then *

For example:

eliot@penguin:~$ rm * <CTRL>+<x> <*>

eliot@penguin:~$ rm Desktop Documents Music Pictures Projects Scripts Videos notes.txt rail_guns.pdf

This is almost certainly the safest and clearest option, but may not immediately become a habit. Hopefully you pause long enough when running rm commands that you’ll think twice and pick up the habit soon enough.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(to be displayed above your comment)

(for moderation; your e-mail address will not be made public)