Prevent rm disasters

Oct 03, '05 05:26:00AM

Contributed by: macgruder

The rm command can be quite dangerous, with just a simple slip of the finger (please do not try this at home!):

$ rm -rf php5 *
$ wtf i meant rm -rf php5*
-bash: wtf: command not found
What's just happened there is that the space before the * will result in the permanent removal of everything at and below the currently active directory, including all sub-directories. Ouch. So make use of !$ instead. This feature takes the argument to the last command and applies it to the current command. For instance:

$ ls php5*
// expected 
// list of
// files here

$ rm -rf !$
rm php5*
Now if you make a mistake in the ls command, you'll see the unexpected output before you run the rm command:
$ ls php5 *
// unexpectedly huge
// list of many
// many files here
It will list everything, and you'll notice your mistake before it's too late.

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050928082624470