Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Force 'rm' to run in interactive mode per directory UNIX
Using the 'rm *' command can be dangerous, especially if you use it in a directory with '*'. If you wish to prevent the accidental deletion of one of your directores' contents, here's how. Just create an empty file called '-i' (use echo "" > -i) in the directory you wish to protect. When you subsequently use rm * in this directory, rm will prompt you to delete each file, exactly as if you had entered rm -i file1 file2 ....

[Editor's note: This is a useful trick; I've now added the -i file to a couple of my folders that I visit often in the Terminal, just in case! And if you need or want to remove the oddly named "-i" file, the easiest way is probably typing "rm ./-i".]
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

Force 'rm' to run in interactive mode per directory | 18 comments | Create New Account
Click here to return to the 'Force 'rm' to run in interactive mode per directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Try just a touch
Authored by: dvreid on Oct 14, '02 11:28:24AM

You can also create the file using the touch command. The odd syntax is
needed due to the dash just like the rm command:

touch ./-i


[ Reply to This | # ]
Use an alias
Authored by: Mike Klein on Oct 14, '02 11:33:15AM

In .bashrc, .tcshrc or .cshrc, add a line such as:

alias rm="rm -i"

Then whenever the rm command is entered, rm -i will be executed.

Safer and easier.

[ Reply to This | # ]
Use an alias
Authored by: osxpounder on Oct 14, '02 01:32:57PM

I guess that one nice thing about using the ./-i file is that it can travel with the folder .... so that, if another user tries to "rm *", and she doesn't have an alias set up to protect her, the ./-i file will provide that last-ditch warning before she deletes files ..... no?

I notice this only protects a user if a glob construct[*] is used. If I have a file named 'blah' in the same folder as a file named "-i", I can still delete without protection by typing "rm blah" ... so that's a good reason to put the "rm -i" alias in my own .*rc file ....

Thanks for a potentially very useful tip!

[*] note: now that I think about it, I'm not sure if the asterisk is a glob construct or not. I may misunderstand the term.

[ Reply to This | # ]
Use an alias
Authored by: crackpip on Oct 14, '02 08:56:35PM

In general it isn't recommended to alias rm -i as rm. The reason for this is that you can get used to relying on it and if you move to another machine or some other situation where your resource file isn't loaded, then you can make a big mistake. Instead its recommended to use an alias like,

alias rmi `rm -i`

[ Reply to This | # ]
Use an alias
Authored by: charles488 on Oct 16, '02 08:26:25PM

Another safer option is to use the alias del 'rm -i' instead of alias rm 'rm -i'. You might also want to add the aliases copy 'cp -i' and move 'mv -i' while
your at it which will provide the same type of warning for the cp and mv commands.

[ Reply to This | # ]
VERY bad idea
Authored by: deleted_user17 on Oct 15, '02 04:51:23AM

One day you will sit on a OS X box without this alias and enter "rm -rf /" - 2 min. later the whole installation is gone...

Never, never, never use this alias!

[ Reply to This | # ]
VERY bad idea
Authored by: gnuadam on Oct 15, '02 09:56:35AM

Uh, even if you alias rm -i with rm, if you type rm -fr /*, you'll still toast your machine. The "-f" flag (which forces the delete to occur without any prompting) seems to override the "-i" flag (which asks the user to confirm deletion). I typed rm -ifr <file>, and it deleted without prompting.

[ Reply to This | # ]
Use an alias
Authored by: Glanz on Oct 15, '02 08:43:49AM

Excellent point! This is the method I have been using for years with the BSDs. It especially helps when people are "talkinatcha" while you are typing! :)
This is one of the best tips for an ADMIN I have seen on these boards.

[ Reply to This | # ]
beware of filenames starting with a space
Authored by: hayne on Oct 14, '02 02:10:14PM
This trick relies on the sorting order of filenames. If you happen to have a filename which starts with a character that comes before the '-' character in the sort order, this trick will not work since that filename will come earlier in the command line than the -i and hence the -i will not be recognized as an to rm. Perhaps the most common case when this might trip you up is if a filename started with a space. It is quite easy to do this (even inadvertantly) in the Finder. You can also create such a file in Terminal by using quotes. For example:
    touch " foo"
creates a file called " foo" (with a space as the first character). You can see what trouble this would cause by looking at the result of:
    echo *
That shows that the file " foo" comes before the file "-i" in the list. You can avoid most such difficulties by creating your "-i" file with a bunch of extra spaces in front of its name:
    touch "             -i"

[ Reply to This | # ]
-i might work with rm, but ...
Authored by: patashnik on Oct 14, '02 03:04:15PM
Try running ls * in a directory with a file named -i; you'll suddenly get inode-listings. There is other stuff which can break, or at least will behave seemingly unexpected, when you go around putting files starting with a hyphen in directories.

[ Reply to This | # ]
Shell variable rmstar
Authored by: adrian mayo on Oct 14, '02 03:23:43PM

One can set the tcsh shell variable rmstar
set rmstar
as a catch-all reminder against typing rm *

This also protects against hitting a space in the wrong place turning
rm *.bak
rm * .bak

[ Reply to This | # ]
Shell variable rmstar
Authored by: thatch on Oct 15, '02 12:54:12PM

Adrian Mayo, you have the best solution for this hint using the tcsh shell.

man tcsh and search for rmstar for detail.

[ Reply to This | # ]
Oddest thing I've seen!
Authored by: dzurn on Oct 14, '02 05:31:21PM

This has to be the oddest *nix tip I've seen yet. It took me a while, then I read closely to find that the "rm *" is expanded to "rm file1 file2..." so the file named "-i" will act as a command switch when the expanded command is finally executed.

It seems to me that this tip would generalize to any command with a "*" wildcard. So if you had a file named "-rf" and you typed "rm -i *" would it override the "-i" and remove everything without interaction? Volunteers? I would never have imagined that file names can add switches to commands. Something new every day....

[ Reply to This | # ]
Authored by: jpkelly on Oct 14, '02 10:45:32PM

I once created a directory by accident thaat began with a "-" I could not delete the #%@$ thing!
Short of gasoline and a match I was tearing my hair out trying to get rid of it.
(not to mention I was trying to impress my roomate with my unix skills on his machine)
I forget what I did to get rid of it.
Perhaps I booted into 9 and trashed it.

[ Reply to This | # ]
deleting a file starting with a hyphen
Authored by: colin on Oct 15, '02 05:43:44AM

the command to remove a file starting with a hyphen is 'rm -- file' the -- switches off the flags so that the filename you type is treated as a filename rather than a switch.

[ Reply to This | # ]
deleting a file starting with a hyphen
Authored by: hayne on Oct 15, '02 10:01:55AM

An easier way (also useful for deleting files with spaces in their names, etc.) is to put the filename in double-quotes. For example:
rm "-i"
would remove the file which is the subject of this hint.

[ Reply to This | # ]
deleting a file starting with a hyphen
Authored by: SeanAhern on Oct 15, '02 03:33:05PM

Nope, that wouldn't work. All the quotes would do is tell the shell that -i is one word. To the rm binary, "-i" and -i look the same.

Believe me, I've tried stuff like this before.

Other ways to do it, though:

rm ./-i
rm -- -i

[ Reply to This | # ]
good idea, but wrong way to do it
Authored by: pmbuko on Oct 15, '02 02:08:00AM
This is was shell configuration scripts are for. If you use tcsh, open or create a file called .tcshrc. If you use bash, open or create .bash_profile or .bashrc. Add the following line:

alias rm='rm -i'

After you've saved your shell profile, you must close the current terminal window and open a new one for the changes to take place.

I'd advise against using file names beginning with a '-', as any command run with a wildcard on the directory containing that file will interpret it as an option (as mentioned above).

As a side note, once you've created the rm -i alias, you can bypass it when you're sure about what you're deleting precedign the command with a backslash:

> rm filename

This trick works to bypass all aliases.

[ Reply to This | # ]