0

I alias rm to rm -i so that when I mistype file* as file * I get prompted before accidentally deleting files I didn't intend to delete. Is there an equivalent idiom for directories?

In particular, to delete a directory and its contents, you have to rm -r. Using rm -ri will prompt you for all the files in it, not just the top-level directory. For directories with a lot of files, that's not convenient. To avoid that, I frequently use rm -rf, but that scares me. I'm only a typo away from blowing away lots of important stuff (e.g. rm -rf ~ /foo instead of rm -rf ~/foo—ouch!).

One could write a mildly annoying script to replace, e.g., rmdir with something that only prompts for the things listed on the command line, but it seems like this is the kind of problem for which a solution should already exist.

Kazark
  • 979
  • 3
  • 12
  • 31
cape1232
  • 103
  • 3
    If would personally never do rm -rf ~/foo, but by default cd ~; rm -rf foo/ I think it is mostly a matter of good habbits. – Bernhard Mar 13 '13 at 18:20
  • I like that. Good advice for that one case. But the problem still exists. For example 'rm -rf _old'. What if you mistype 'rm -rf _old'? – cape1232 Mar 13 '13 at 18:36
  • 2
    @Bernhard except you want && instead of ;, otherwise you way delete the wrong directory if the cd fails for some reason. – jordanm Mar 13 '13 at 18:53
  • @jordanm I'd normally not type them on a single line obviously. – Bernhard Mar 13 '13 at 21:34

3 Answers3

5

rm -i would get extremely annoying especially when removing recursively.

rm -I, on the other hand seems a little more tolerable which will prompt once before removing more than three files, or when removing recursively.

It's much less intrusive while still giving protection against most mistakes.

The best form of protection is to double/triple check when deleting recursively or better yet, anything you delete.

cinelli
  • 1,339
  • 8
  • 17
  • 1
    In my experience, this kind of aliases just makes the "rm fooy" automatic... i.e., no protection at the cost of 2 extra keystrokes. And it isn't available everywhere. Better don't do that, except for the greenest training-wheel-needy users. – vonbrand Mar 13 '13 at 20:20
  • Seeing that rm foo would only remove one file. rm foo would only ask once. If you're talking about when removing the directory foo/ then the command rm -Ir foo would ask once as well (if it contained more than 3 files). If you were to remove a directory recursively that had 1000 files in it (for arguments sake). Then you'd be there for quite a bit of time saying Yes to confirm all the files with -i where since the user should know what files their deleting -I will ask once for any removal operation with three or more files involved. – cinelli Mar 13 '13 at 22:41
  • Adding to that. If the user doesn't know what's contained in foo/ then when rm -Ir foo is passed and it asks "You sure?" and the user says Yes without double checking and not knowing what's in the directory. Then, that's something nobody can answer other than. "Pay attention to what you're doing to you're system. Or go back to using the Recycle Bin in Windows." – cinelli Mar 13 '13 at 22:47
0

One option is to use a trash can approach, like here.
There are a lot of examples out there besides this one - it was just the first in my search results. Basically, all rm operations become mv commands. If you rm the wrong files, they're still on the machine and easily recoverable. And the approach linked above creates an alias to the base rm command so you can still use it when you really want to.

user17591
  • 1,098
0

You can cd to the top level directory you want to remove recursively, then go up a level and rm that directory. That way you don't have to use leading slashes, etc.

cd /home/user/junk
ls -1

folders
to
delete

rm -ri . ###Or "rm -rf ."
cd ..
rm -rf junk

Another way to check that your paths are right for any command is to simply use tab completion.

gNU.be
  • 1,306