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

Bad Idea

Using rm to move files to trash is like weed. It is common and pleasuring but can be bad for you in the future. ;)

You really need control yourself when using rm.


Don't use rm

Imagine, you get used to rm moving to trash and make a habit of it. Sure, your system is safe but when you log into a friend's (or your wife's or your boss') notebook and have to delete something? You'll be actually using the real rm - deleting those files forever. It's a bad habit, and you need to know that.

So instead, install rmtrash and make a habit of using it:

# install rmtrash, (either from the macports or by the brew.)
$ alias trash="rmtrash"
$ alias   del="rmtrash"       # del / trash are shorter than rmtrash

Correcting bad habits

Another thing you can do to force yourself to use rmtrash instead of rm is alias it to a custom message in your .profile.

alias rm="echo Use 'del', or the full path i.e. '/bin/rm'"

So, whenever you use rm, you'll be prompted to either use rmtrash or /bin/rm. Remember, this is only temporary, after a while you should remove the alias.


Never ever, do something like this:

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

Pop Quiz: In what directory will the rmcommand run? :)

Ps: Just removed some unwanted edits to this post, what added some nonsenses about the aliases.


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


There are ways to protect yourself from horrible outcomes from rm. However, I agree with the other commenters here: very few are any good.

Do not for a minute believe that changing the command environment will help you. For example, you will find people online everywhere recommend doing this sort of thing:

alias rm="rm -i"

or

function rm()
{
    echo >&2 'rm forbidden; use "/bin/rm"'
}
export -f rm

This will not work. Period. There are loads of use cases where these little parlor tricks will fail you miserably.

The only robust way I have used to spare myself from the danger of rm is by setting up a regular backup process through cron over the directories where I work. Raw UNIX does it for you nicely, with great simplicity, using find in conjunction with -mtime to check for files that have been updated within or since a certain time.

Tags:

Console

Trash