25

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.

  • 7
    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 http://stackoverflow.com/questions/373156/what-is-the-safest-way-to-empty-a-directory-in-nix? – David Harris Jan 25 '12 at 17:43
  • @grossvogel it happened to me once in 5+ years in which I use Linux and the terminal every day. –  Jan 25 '12 at 17:47
  • @puller: Sorry, I wasn't trying to say you're a bad linux user. Just that the design of rm is already quite good, in that it already has safety checks, and only becomes overly destructive when you disable them. – user26512 Jan 25 '12 at 17:52
  • 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. –  Jan 25 '12 at 17:58
  • 27
    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
  • Good times if it's not your system. Assuming you can outrun them. – Sirex Jan 27 '12 at 10:34
  • 2
    @puller Only on systems like RHEL that stupidly alias rm to rm -i. – Daniel Beck Jul 21 '13 at 09:03

8 Answers8

32

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 automatically, you can recover the files at the state they were during the last snapshot before the incident.  

yanot
  • 117
jlliagre
  • 14,179
18

If you want save aliases, but don't want to risk getting used to the commands working differently on your system than on others, you can to disable rm like this

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

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

alias remove='/bin/rm -irv'

or use trash instead.

Dario Seidl
  • 3,965
  • 2
    Note that (at least in some versions) you must define the aliases in the other order.  Doing it in the order you present defines remove as echo "rm is disabled, use trash or /bin/rm instead." -irv. And, just as a matter of common sense, wouldn't you want the message from rm to refer to your safe remove command? – Scott - Слава Україні May 03 '17 at 17:39
  • You're right, I'll update the answer accordingly. – Dario Seidl May 03 '17 at 22:15
  • Why this answer is correct for me: it protects you against using rm by mistake (I have literally typed rm instead of mv - and more than once), but, It does not have the pitfalls of rm -i alias that are discussed in other answers/comments. – Rodney May 24 '21 at 11:18
6

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

wisbucky
  • 3,076
l0b0
  • 7,351
4

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'? 
  • Won't work if the user decides to use * . * instead of just *. But still a neat trick. – Peon Jan 27 '12 at 05:03
  • 1
    afaik rm -rf . nor rm -rf .. can't succeed because you are currently in the directory. – Peon May 20 '12 at 18:10
  • @Peon: I believe that you mean .* *, rather than * .* (which I believe to be more common) or * . * (which you said). … … … … … … … … … … … … … … … … … … … … … … … … … … For this technique to be truly, globally effective, you would need to do it in every directory where you have write permission.  But I agree, it is a neat trick. – Scott - Слава Україні May 03 '17 at 17:28
1

I use the following script.

#!/bin/sh

trash=$HOME/tmp
mv "$@" $trash
nohup find "$trash" -type f -atime +7 -exec /bin/rm '{}' \; 2>&1 &

If you accidentally delete a file, recover it from the $HOME/tmp.

The script moves deleted files to a tmp directory and deletes them next time the delete script is run if the access time is 7 days later (semi-automated cleanup of the $HOME/tmp directory).

techraf
  • 4,902
-2

This bash code adds the function rm to your ~/.bash_profile configuration file. After running this, if you write rm file, you will move that file to /tmp

cat << EOF >> ~/.bash_profile
rm () {
  mv $1 /tmp
}
EOF
Nifle
  • 34,446
-2

In your profile,

alias rm="rm -i"
  • 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 02:41
  • rm -I gives the best of both worlds, @jlliagre. – Tamara Wijsman Jul 16 '12 at 02: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 07:05
  • @jlliagre: Hmm, so you mean scripts do catch this change? – Tamara Wijsman Jul 16 '12 at 12:24
  • Please read my reply http://superuser.com/a/382498/19279 to get my point. – jlliagre Jul 16 '12 at 15:14
  • In addition, alias rm="rm -i" won't change puller's case, as the resulting command would have been rm -i -rf * which doesn't prompt the user for deletion anyway. alias rm=rm -I isn't better. It will remove unconditionally all the files and directories. – jlliagre Jul 16 '12 at 15:20
-2

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.

beefus
  • 1
  • 2
    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 08:55
  • 2
    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 08:12