Protect directory from an overzealous rm -rf *

cd <directory>; touch ./-i
Forces the -i flag on the rm command when using a wildcard delete.
Sample Output
root@server ~ # cd scripts; touch ./-i
root@server ~/scripts # rm -rf *
rm: remove regular empty file `file1'? n
rm: remove regular empty file `file2'? n
rm: remove regular empty file `file3'? n
rm: remove regular empty file `file4'? n
root@server ~/scripts #

By: ljmhk
2011-05-12 11:01:58

1 Alternatives + Submit Alt

What Others Think

kaedenn · 544 weeks and 4 days ago
Great. But why should you change directory? Just do the: touch /path/-i
netizen · 544 weeks and 3 days ago
I'm constantly tripping over the 'alias rm="rm -i"' that gets forced on me with Fedora/Debian Linux distros. I like this idea for a per-directory molly-guard.
Mozai · 544 weeks and 3 days ago
Unfortunately it doesn't save from rm -fr ./* or when deleting parent dirs: touch ./path/-i rm -fr *
aikikode · 544 weeks and 3 days ago
Very interesting but doesn't seem to work here on a Mac nor a CentOS 5.3 machine using bash 3.2 david@host /tmp $ cd a david@host /tmp/a $ mkdir ta tb tc david@host /tmp/a $ touch ta/-i david@host /tmp/a $ rm -rf * david@host /tmp/a $ ls david@host /tmp/a $
DaveQB · 544 weeks and 3 days ago
DaveQB ... did you read aikikode's comment? Works fine for me on Mac OSX.
unixmonkey21519 · 544 weeks and 3 days ago
My vote is up, but still it will not protect against rm -rf -- * Nice thinking though.
RanyAlbeg · 544 weeks and 3 days ago
safe-rm ( is another way of preventing accidental deletions through a blacklist of paths you don't want to delete (e.g. /bin, /usr/lib or ~/important_file.gpg)
fmarier · 544 weeks and 3 days ago
fmarier, that looks interesting I'll take a look. oh and thanks for the up votes guys, its not perfect but handy when training the junior sys admins.
ljmhk · 544 weeks and 3 days ago
If you really want to protect files from being deleted or change, even by root, use the chattr command: chattr +i Only works on ext2 systems.
laebshade · 544 weeks and 2 days ago
@unixmonkey21519 aikikode Must have posted seconds before me as I did not see it just before or I posted (or even after actually)
DaveQB · 544 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: