Blog Archive

To get the "rm" (but you can alias some other command like "del" or keep the original "trash" command!) command to move the file(s) to trash instead of removing them completely, you must install the trash-cli utility and set an alias for "rm" to use trash-cli. But trash-cli also removes folders, even without using the "-r" (recursive) option, so in this post I will share a script created by grizzly which will only remove folders when the "-r" argument is used.

Here is the complete mini-guide to setting "rm" to move files (and folders) to trash:

1. Install trash-cli

This package provides a command line interface trashcan utility compliant with the FreeDesktop.org Trash Specification. It remembers the name, original path, deletion date, and permissions of each trashed file.

Update: starting with Ubuntu 12.04 Precise Pangolin, trash-cli provides the command "trash-put" to trash files, instead of the previous "trash" command, so in the code above, on the last line, replace "trash" with "trash-put" for Ubuntu 12.04+.

Then make it executable by opening a terminal and running this:

sudo chmod +x /usr/local/bin/trash-rm

3. Create an alias for "rm" to use "trash-rm"

In Ubuntu, run this in a terminal:

gedit ~/.bashrc

and enter this at the end of the file:

alias rm="trash-rm"

and save it.

Then reload bashrc by running the following command in a terminal:

bash

That's it! Now try it out by deleting files the way you always do, using "rm" and "rm -r".

Final trash-cli tips

Since you've installed the trash-cli utility, now you can use the following commands for manipulating the trash from the command line (the names are self explanatory):

empty-trash
list-trash
restore-trash

In the latest trash-cli (Ubuntu 12.04; trash-cli version 0.11.3+git20120324), the commands have changed: