Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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
1  
@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
add comment

3 Answers 3

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|improve this answer
1  
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. –  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
add comment

One option is to use a trash can approach, like here: http://hash-bang.net/2009/03/trash-with-bash/ 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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.