Prevent Accidental Deletion with rm -rf *

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
ls
-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.

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.