Best practices to alias the rm command and make it safer

21

8

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.

Vincenzo Pii

Posted 2012-01-25T17:36:15.797

Reputation: 313

7Not 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 – 2012-01-25T17:40:51.830

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 – 2012-01-25T17:43:44.170

@grossvogel it happened to me once in 5+ years in which I use Linux and the terminal every day. – None – 2012-01-25T17:47:31.713

@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 – 2012-01-25T17:52:29.223

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. – None – 2012-01-25T17:58:46.793

24Note 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 – 2012-01-25T19:41:02.790

Good times if it's not your system. Assuming you can outrun them. – Sirex – 2012-01-27T10:34:40.797

2@puller Only on systems like RHEL that stupidly alias rm to rm -i. – Daniel Beck – 2013-07-21T09:03:34.107

Answers

31

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.  

jlliagre

Posted 2012-01-25T17:36:15.797

Reputation: 12 469

3i've heard that horror story personally from one of those that suffered it. – Dan D. – 2012-01-26T03:38:39.490

I agree but most people aren't at liberty to switch to an entirely different filesystem. – Peon – 2012-01-26T06:40:27.637

Indeed, that's the reason why I first suggest doing backups, a feature no file system forbids. – jlliagre – 2012-01-26T13:32:38.360

12

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

Posted 2012-01-25T17:36:15.797

Reputation: 2 409

2Note 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 – 2017-05-03T17:39:39.660

You're right, I'll update the answer accordingly. – Dario Seidl – 2017-05-03T22:15:53.840

5

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

l0b0

Posted 2012-01-25T17:36:15.797

Reputation: 6 306

3

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'? 

DarkHeart

Posted 2012-01-25T17:36:15.797

Reputation: 191

@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 – 2017-05-03T17:28:26.490

Won't work if the user decides to use * . * instead of just *. But still a neat trick. – Peon – 2012-01-27T05:03:54.230

1afaik rm -rf . nor rm -rf .. can't succeed because you are currently in the directory. – Peon – 2012-05-20T18:10:35.307

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).

user580224

Posted 2012-01-25T17:36:15.797

Reputation: 11

-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

Vicente Gonzalez

Posted 2012-01-25T17:36:15.797

Reputation: 1

1What this script/code do? Please explain so less-technical users can understand your answer. – Vylix – 2017-05-03T16:07:14.313

2While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill – 2017-05-03T17:10:29.043

1This does provide an answer to the question, but it's a very poor one.  Two better versions of this answer have been posted already. – Scott – 2017-05-03T17:44:37.023

This bash code adds the function rm to your ~/.bash_profile configuration file. After running this file, if you write rm file, you will move that file to the /tmp. – Vicente Gonzalez – 2017-05-04T09:36:25.463

Thanks for explaining. Could you please [edit] your answer to include that in the post itself? – Ben N – 2017-05-04T13:13:01.143

-2

In your profile,

alias rm="rm -i"

schtever

Posted 2012-01-25T17:36:15.797

Reputation:

3Aliasing rm is, in my opinion, quite a poor advice and it won't help anyway in puller's case. – jlliagre – 2012-01-26T02:41:56.197

rm -I gives the best of both worlds, @jlliagre. – Tamara Wijsman – 2012-07-16T02:35:22.200

@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 – 2012-07-16T07:05:41.377

@jlliagre: Hmm, so you mean scripts do catch this change? – Tamara Wijsman – 2012-07-16T12:24:08.090

Please read my reply http://superuser.com/a/382498/19279 to get my point.

– jlliagre – 2012-07-16T15:14:24.710

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 – 2012-07-16T15:20:12.237

-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

Posted 2012-01-25T17:36:15.797

Reputation: 1

2This 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 – 2013-07-21T08:55:22.227

2Thanks 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 – 2013-07-23T08:12:30.437