Safe Directory Delete
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.
Related videos on Youtube
user3413206
Updated on September 18, 2022Comments
-
user3413206 almost 2 years
I alias
rm
torm -i
so that when I mistypefile*
asfile *
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
. Usingrm -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 userm -rf
, but that scares me. I'm only a typo away from blowing away lots of important stuff (e.g.rm -rf ~ /foo
instead ofrm -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 over 11 yearsIf would personally never do
rm -rf ~/foo
, but by defaultcd ~; rm -rf foo/
I think it is mostly a matter of good habbits. -
user3413206 over 11 yearsI 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 over 11 years@Bernhard except you want
&&
instead of;
, otherwise you way delete the wrong directory if thecd
fails for some reason. -
Bernhard over 11 years@jordanm I'd normally not type them on a single line obviously.
-
-
vonbrand over 11 yearsIn 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 over 11 yearsSeeing 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 commandrm -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 sayingYes
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 over 11 yearsAdding to that. If the user doesn't know what's contained in
foo/
then whenrm -Ir foo
is passed and it asks "You sure?" and the user saysYes
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."