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
[email protected] ~ # cd scripts; touch ./-i
[email protected] ~/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
[email protected] ~/scripts #

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

1 Alternatives + Submit Alt

What Others Think

kaedenn · 506 weeks and 3 days ago
Great. But why should you change directory? Just do the: touch /path/-i
netizen · 506 weeks and 2 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 · 506 weeks and 2 days ago
Unfortunately it doesn't save from rm -fr ./* or when deleting parent dirs: touch ./path/-i rm -fr *
aikikode · 506 weeks and 2 days ago
Very interesting but doesn't seem to work here on a Mac nor a CentOS 5.3 machine using bash 3.2 [email protected] /tmp $ cd a [email protected] /tmp/a $ mkdir ta tb tc [email protected] /tmp/a $ touch ta/-i [email protected] /tmp/a $ rm -rf * [email protected] /tmp/a $ ls [email protected] /tmp/a $
DaveQB · 506 weeks and 2 days ago
DaveQB ... did you read aikikode's comment? Works fine for me on Mac OSX.
unixmonkey21519 · 506 weeks and 2 days ago
My vote is up, but still it will not protect against rm -rf -- * Nice thinking though.
RanyAlbeg · 506 weeks and 2 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 · 506 weeks and 2 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 · 506 weeks and 2 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 · 506 weeks and 1 day 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 · 505 weeks and 6 days 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: