Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Some time ago I erroneously deleted my home folder because I ran a rm -rf * on the wrong terminal, whose working directory was the home folder!

I wish I had an alias for the rm command, but it was not the case.

Now, I am planning to make a script as an alias for rm.

Do you have any best practice to suggest?

Thanks.

share|improve this question

migrated from stackoverflow.com Jan 25 '12 at 20:46

This question came from our site for professional and enthusiast programmers.

5  
Not that I don't have sympathy, but I don't think rm is a dangerous command requiring an alias for safety. By using the r and f options, you're telling the shell I know this is dangerous, and I've thought carefully about it, and it's _really_ what I want to do. If you don't use those options, you can't delete your entire home folder with it. –  user26512 Jan 25 '12 at 17:40
    
Does this question cover the same information as stackoverflow.com/questions/373156/…? –  David Harris Jan 25 '12 at 17:43
1  
@grossvogel no offense taken :)! However, aliasing this command is a common practice. A lot of people use the -f option to avoid confirming each deletion. –  puller Jan 25 '12 at 17:58
16  
Note that if you use an alias for rm you will get used to that alias. Then one day, you'll be on a system where the alias doesn't exist, and you'll do the wrong thing when you are least expecting it. And it probably won't be your system. –  Stefan Lasiewski Jan 25 '12 at 19:41
1  
@puller Only on systems like RHEL that stupidly alias rm to rm -i. –  Daniel Beck Jul 21 '13 at 9:03
show 3 more comments

6 Answers 6

up vote 17 down vote accepted

If you want a customized rm, don't call it rm but a name of yours, myrm, delete or whatever.

The rm='rm -i' alias is an horror because after a while using it, you will expect rm to prompt you by default before removing files. Of course, one day you'll run it with an account that hasn't that alias set and before you understand what's going on, it is too late.

In any case, a good way to be prepared for file loss or corruption is doing backups.

A fast alternative that will protect you against accidental file deletion or overwriting is using a file system that support unlimited snapshots like ZFS. If frequent snapshots are done done automatically, you can recover the files at the state they were during the last snapshot before the incident.

share|improve this answer
    
i've heard that horror story personally from one of those that suffered it. –  Dan D. Jan 26 '12 at 3:38
    
I agree but most people aren't at liberty to switch to an entirely different filesystem. –  Peon Jan 26 '12 at 6:40
    
Indeed, that's the reason why I first suggest doing backups, a feature no file system forbids. –  jlliagre Jan 26 '12 at 13:32
add comment

I suggest to disable rm like this

alias rm='echo "rm is disabled, use trash or /bin/rm instead."'

Then you can create your own safe alias, e.g.

alias remove='rm -irv'

or use trash instead.

share|improve this answer
add comment

You could try using trash instead. Just remember to empty it once in a while...

share|improve this answer
add comment

Without having to change everyones profile, what you can do is place file called -i in the directory.

# touch -- -i
# ll
total 0
-rw-r--r-- 1 root users  0 Jan 26 19:24 files
drwxr-xr-x 2 root users 40 Jan 26 19:24 folder_of_power
-rw-r--r-- 1 root root   0 Jan 26 19:25 -i
-rw-r--r-- 1 root users  0 Jan 26 19:24 important
-rw-r--r-- 1 root users  0 Jan 26 19:24 very
# rm -rf *
rm: remove regular empty file `files'? 
share|improve this answer
    
Won't work if the user decides to use * . * instead of just *. But still a neat trick. –  Peon Jan 27 '12 at 5:03
    
. and .* should both NEVER be used on a UNIX machine. As they will both match the two hard links . and .. which will destroy everything if run with a -rf as root. –  DarkHeart Jan 28 '12 at 3:16
1  
afaik rm -rf . nor rm -rf .. can't succeed because you are currently in the directory. –  Peon May 20 '12 at 18:10
add comment

In your profile,

alias rm="rm -i"
share|improve this answer
3  
Aliasing rm is, in my opinion, quite a poor advice and it won't help anyway in puller's case. –  jlliagre Jan 26 '12 at 2:41
    
rm -I gives the best of both worlds, @jlliagre. –  Tom Wijsman Jul 16 '12 at 2:35
    
@Tom Wijsman, partially indeed. Changing the behavior of a critical standard Unix command is still a potential risk here. If you want it to be interactive, give the alias a different name. –  jlliagre Jul 16 '12 at 7:05
    
@jlliagre: Hmm, so you mean scripts do catch this change? –  Tom Wijsman Jul 16 '12 at 12:24
    
Please read my reply superuser.com/a/382498/19279 to get my point. –  jlliagre Jul 16 '12 at 15:14
show 1 more comment

A script called d, with contents mv $* /tmp.

edit: This is a bad idea; see below. I currently have alias d='mv -t /tmp' in my .profile instead.

share|improve this answer
1  
This suggestion is dangerous and could a) override already trashed files with the same name or b) delete the completely wrong files (d "foo bar" will delete the files foo and bar). Something safer is probably #!/bin/bash TRASH="$( mktemp -d -t trashed.XXX )"; echo "Moving to trash $TRASH"; mv -v -- "$@" "$TRASH" (mktemp and mv syntax depends on your system, this one works for Linux). –  Daniel Beck Jul 21 '13 at 8:55
    
Thanks for the explanation. I'm fine with issue a) but issue b) is a big problem. I changed my d command to alias d='mv -t /tmp' in my .profile; I think that's the simple solution I was looking for. –  beefus Jul 23 '13 at 8:12
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.