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.

link|improve this question
2  
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 at 17:40
Does this question cover the same information as stackoverflow.com/questions/373156/…? – David Harris Jan 25 at 17:43
@grossvogel it happened to me once in 5+ years in which I use Linux and the terminal every day. – puller Jan 25 at 17:47
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 at 17:58
9  
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 at 19:41
show 2 more comments
feedback

migrated from stackoverflow.com Jan 25 at 20:46

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

5 Answers

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 will be 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.

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

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'? 
link|improve this answer
Won't work if the user decides to use * . * instead of just *. But still a neat trick. – Peon Jan 27 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 at 3:16
afaik rm -rf . nor rm -rf .. can't succeed because you are currently in the directory. – Peon May 20 at 18:10
feedback

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.

link|improve this answer
feedback

In your profile,

alias rm="rm -i"
link|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 at 2:41
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown