UNIX is a powerful system; when you tell it to do something, it does it. That's why people use GNU Linux and BSD and Solaris systems; they like being in control of their computer.

On the other hand, when a computer blindly does what you tell it to do, you'd better be sure you want to do it. Especially when you issue the dreaded "rm" command, which tells your computer to remove a file.

On the command line, there is no trashcan. If you tell a computer to remove a file, it removes the file, no questions asked. Sound like a bad idea? Sometimes it is! Happily, there is trashy, the sane and portable rm intermediary.

What Is trashy?

Trashy is two simple shell scripts that do two things:

It is written in BASH but should be fairly portable and will run on any UNIX or UNIX-like system without additional dependencies to install.

How Do I Install?

  1. Currently you can grab trashy from its git repository by issuing this command:

    git clone git://gitorious.org/trashy/trashy.git

  2. Now all you need to do is install it. On Slackware or Fedora or Debian proper:

    cd ./trashy && su -c ./install.sh

    On Ubuntu or 0S X and some others:

    cd ./trashy && sudo ./install.sh

  3. And finally you might want to temporarily alias rm to trash because unless you're quite new to Unix, you probably have fingers trained to type "rm" when you really mean trash. To solve this, you can make an alias so that when you accidentally type rm, the file is trashed instead of deleted forever. On most Linux distributions you'd do this:

    echo "alias rm='/usr/local/bin/trash'" >> ~/.bashrc && source ~/.bashrc

    Note that this is a personal alias and will not interfere with the root user's usage of rm, but if you are still hesitant to interfere with such a low-level, common command, then at least alias rm to 'rm -i'

And that's all there is too it! No more accidental, irreversible deletion of files!

Note: Since sometimes you will probably really want to delete a file, you can always access the actual rm command by typing in the full path: /bin/rm (but use it sparingly; remember the reason you installed trashy in the first place was to avoid dangerous rm mishaps!)

How do I uninstall?

If you mean you want your unadulterated rm command back, you can do that by removing the alias:

sed -i 's_alias rm_#alias rm_' ~/.bashrc

But if you want to remove trashy entirely from your system then simply remove them, as they are independent scripts:

su -c '/bin/rm /usr/local/bin/{trash,empty}'

This sounds too good to be true.

It is. This is a very simple shell script written in BASH. It's main advantage is that it is extremely portable; you can install it on your Linux box, your BSD box, your Solaris box, and even your mom's 0S X box, and it will Just Work.

The [dis?]advantage is that it's basic. Most notably, it cannot understand spaces in file names, even if you escape them properly, so if you are deleting files with spaces in their names, you must do so manually or you must simply rm them.

Another advantage to trashy is that it is simple. At its most basic, it creates a ~/.trash folder and mv's the files you trash into that directory. When you issue the command empty then it actually rm's those files.

Can I Integrate trashy With KDE or GNOME?

By default, trashy creates the .trash folder in your home directory. This means that if you are using the GNOME3 desktop environment then the file will automagically appear in its the GNOME trash.

To integrate it into your KDE4 trashcan, simply change the $BASKET variable to the location of your graphical environment's trash folder.

Assuming you've already installed trashy and now want to integrate it into your KDE trashcan, do this as root:

sed -i 's_$BASKET=~/.trash_$BASKET=~/.local/share/Trash/files_' /usr/local/bin/trash

And now when you are trashing items, they are moving into the location also used by your graphical trash can. Unless you know what you are doing (and if you do, you aren't reading this) then I recommend NOT changing the empty script to manage the graphical Trash location; in other words, if you're integrating trashy into your graphical environment, just empty the trash the way KDE or GNOME wants you to, don't go scripting it to be emptied from the command line.

GUI Integration on BSD, Solaris, and OS X

GNOME and KDE on BSD and Solaris function the same way, so integration is the same for them as it is with GNU Linux. OS X actually uses per-device trash folders, but it's not the worst hack in the world to set $BASKET=~/.Trash

I Love This But I Want More

If you are looking for a classier commandline trash utility, you may like trash-cli, a fancy Python trash utility with many interesting commands and switches to make your interaction with trash far more exciting.

The disadvanage to trash-cli compared to trashy is that it is a little less portable. Being Python, it can be ported to other systems but it may require a bit of work if your system does not follow freedesktop.org specs (in which case you should, if possible, change your system).

Trash-cli also has a few dependencies that will need to be met before installing.

The advantage to trash-cli is that it is very skillfully written, while trashy is a hack by a guy who doesn't know the difference between a function and a method. Then again, trashy's a lot easier to install and pretty simple to use. So, take your pick.

For the record, the author of trashy installs and uses it in a multimedia production facility that uses Slackware, Debian, Red Hat, and Fedora Linux, freeBSD, and Mac 0S, and it installs and works without a hitch (other than a chsh on freeBSD, but that's one box and it's been GNU'd a looong time ago).

I'm a Real Geek and I love rm. How dare you slander it.

If you love rm, continue using rm. I personally love rm; it's a very effective tool for instantly and permanently erasing a file from the face of this Earth and all possible worlds and timelines. For those who just want a little time apart from their files before eradicating them completely, there's trashy.

Trashy icon by liftarn at openclipart.org