21

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.

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

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

  • 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 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
  • 23
    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
  • 2
    @puller Only on systems like RHEL that stupidly alias rm to rm -i. – Daniel Beck Jul 21 '13 at 9:03
30

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.  

  • 3
    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
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.

  • 1
    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 3 '17 at 17:39
  • You're right, I'll update the answer accordingly. – Dario Seidl May 3 '17 at 22:15
5

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

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'? 
  • Won't work if the user decides to use * . * instead of just *. But still a neat trick. – Peon Jan 27 '12 at 5: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 3 '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).

-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 2:41
  • rm -I gives the best of both worlds, @jlliagre. – Tamara 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? – Tamara 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
-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.

  • 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 8: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 8:12
-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
  • 1
    What this script/code do? Please explain so less-technical users can understand your answer. – Vylix May 3 '17 at 16:07
  • 2
    While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill May 3 '17 at 17:10
  • 1
    This 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 May 3 '17 at 17:44
  • 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 May 4 '17 at 9:36
  • Thanks for explaining. Could you please edit your answer to include that in the post itself? – Ben N May 4 '17 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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