How can I make 'rm' move files to the trash can?

I use the terminal on a daily basis, I will often use rm DirectoryName, sometimes I get a little crazy and do rm *

As you can imagine these shenanigans have sometimes come back and hit me in the face. Does anyone know how I create a mapping so rm whatever moves everything to the trashcan which I then delete manually.

-
you can do mv /file/to/delete ~./Trash –  user6124 Jul 18 '11 at 9:42
It's hard to put training wheels on a precision tool like rm without breaking other scripts. Something like alias rm=/bin/rm -i should get you safely trained out of your crazy. Also - which Trash directory do you want rm to use? It gets clunky since Mac OS X has a trash directory for each volume as well as a user level trash can. –  bmike Jul 18 '11 at 15:53
If you use zsh, by default it will prompt you when you do rm * or rm /path/* –  Daniel Serodio Jun 13 at 22:59

Making "rm" to move to trash is like a weed. It is common, pleasuring now and can be bad in the future. ;) You really need control yourself with using "rm".

Imagine, you get used a habit rm moving to trash. Once you will log into your friend (wife, boss) notebook and you will use "rm". But you will get an normal rm - so you will delete all stuff forever. Bad habit. ;)

alias rm="echo Use del, or full path name for rm"
alias del="rmtrash" #del is shorter as rmtrash, or simply use rmtrash directly


So, when do you use "rm" you got only a message to use the "del" command. When you really want use "rm" you can do it with full path name: /bin/rm

After a while, (when you get a new habit using del routinely) you should remove the "rm" alias.

The result:

• you will get a right habit (using "del" or "rmtrash"), and
• you will think before using an dangerous "rm"

Ps:

Never, never, never do things like:

 cd /tmp; rm -rf *


I know about what I'm talking. ;) Not a nice experience the following

$pwd /$ cd /tnp; rm -rf *
sh: cd: /tnp: No such file or directory


Quiz question? In what directory will run the "rm" command?

-
&& is your lifeline. ; is the devil. P.S. The answer is that pretty much everything on your system disappears, in the above example. –  Jason Salaz Jul 20 '11 at 8:06

I can think of a dozen ways to code this, but categorically refuse to type any of them up in an answer. I strongly recommend you curb your habit of getting a little crazy. You shouldn't be in the habit of using rm at all if you can't use it properly.

The fundamental problem here is that even if you install a safety net on your own system, that will only help you keep your bad habits and when you happen to be on another machine some day that doesn't have the same custom protections, you will do something epicly bad.

The proper solution is to use rm as it was meant to be used including manually adding the -i argument whenever you are in down about how a glob will expand, and have good system backups that you can restore in the event of serious user error. Trying to add "trash" to commands that in the rest of the universe don't use it is a half-way step that is the worst of both worlds.

Either use the trash or don't. Using rm doesn't go to trash, it removes.

If you want to use the trash, there is nothing wrong with that. Just get in the habit of using the rmtrash command instead of rm. This will help your brain understand what's happening and not get you into a bad habit that will cause grief later.

-
Your point doesn't compute in this instance. People delete things accidentally on a daily basis, system backups do not work in the case of a home pc (which mine is) as the resources are limited, a move to trash can is a far more efficient way of handling these deletions for my particular use case. –  steve Jul 21 '11 at 11:11
@steve you missed the point entirely. rm by definition removes a file from the disk. It doesn't send it to the trash. Teaching yourself to use rm when you mean send to trash sets you up for disaster. If you want to send to trash, use rmtrash or a program that was designed for the job, then you won't run into nightmares later. I wasn't saying using the trash is inherently wrong, but if you want to use it, don't wire it up to something that was not intended to use it! –  Caleb Jul 21 '11 at 11:13
alias rm='rmtrash'

If you do use a tool like rmtrash, please do yourself a favor and use it by it's own name. Never alias things that are not rm to rm, it will some day bite you. –  Caleb Jul 18 '11 at 9:45