Prevent Accidental Deletion with rm -rf *

Categories General Info

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.

Leave a Reply

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