Feb 042010

From O’Reilly’s “UNIX Power Tools”
Creating a file named -i will prevent an errant rm -rf * from wiping out the directory containing it. How? The * expands -i to your command line, and your rm -rf * becomes rm -rf -i *!

See it in action:

cd test/
touch -- -i
touch 1 2 3 a b c
-i 1 2 3 a b c
rm -rf *
rm: remove regular empty file `1'?

So how do you actually delete this odd file?

rm -- -i

The ‘–’ option tells it to stop looking for flags and treat the rest of the command line as operands.

Note that this trick won’t work if you specify an absolute path (as in rm -rf ~/test/) — only when you are within the directory. I’d recommend dropping this in your home and / directories.

 Posted by at 3:53 am