Safe Directory Delete

7,663

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.

Share:
7,663

Related videos on Youtube

user3413206
Author by

user3413206

Updated on September 18, 2022

Comments

  • user3413206
    user3413206 almost 2 years

    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.

    • Bernhard
      Bernhard over 11 years
      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.
    • user3413206
      user3413206 over 11 years
      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'?
    • jordanm
      jordanm over 11 years
      @Bernhard except you want && instead of ;, otherwise you way delete the wrong directory if the cd fails for some reason.
    • Bernhard
      Bernhard over 11 years
      @jordanm I'd normally not type them on a single line obviously.
  • vonbrand
    vonbrand over 11 years
    In my experience, this kind of aliases just makes the "rm foo<ENTER>y<ENTER>" 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.
  • cinelli
    cinelli over 11 years
    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
    cinelli over 11 years
    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."