Prevent Accidental Deletion with rm -rf *
March 13, 2008
Spotted this tip on the programming subreddit, and I thought it was too clever not to post.
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:
[mshade@slicebot ~]$ cd test/
[mshade@slicebot test]$ touch — -i
[mshade@slicebot test]$ touch 1 2 3 a b c
[mshade@slicebot test]$ ls
-i 1 2 3 a b c
[mshade@slicebot test]$ 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.
Enjoy!
With credit to probablycorey.
Posted in 

content rss
August 30th, 2008 at 3:29 am
[...] Prevent Accidental Deletions with rm -rf * [...]
September 8th, 2008 at 10:54 pm
>Note that this trick won’t work if you specify an absolute path
Why not to use ‘less rectal’ solutions?:)
apt-get install safe-rm
or (FreeBSD-way):
alias rm=’rm -i’