June 17, 2008

Bringing the trashcan to the command line

Author: Ben Martin

The trash project allows you to interact with your desktop trashcan from the command line. It lets users "undo" deletions made with the trash command in a similar manner to restoring files from the trashcan in a desktop environment. For experienced Linux users, the trash command comes in handy when you want to put a file into the trashcan from the command line.

Because trash implements the FreeDesktop.org Trash Specification, it plays nicely with the trashcan offered by the KDE desktop environment. That means you can trash a directory from the command line and see it in your trashcan from Konqueror. Unfortunately, the trash implementation in GNOME 2.20 did not communicate with either KDE 3.5.8 or the trash command.


Trash is not available from the distribution repositories for Ubuntu, Fedora, or openSUSE. I built version 0.1.10 from source on a 64-bit Fedora 8 machine. Trash is written in Python, so build and installation follows the normal python setup.py install procedure.

When you use the list-trash command to view the contents of your trashcan, you might encounter an error if you are also using the Linux Logical Volume Manager (LVM). Version 0.1.10 of trash uses the df command to work out what filesystems are available. Unfortunately, it invokes df without the POSIX compatibility mode -P, and as such the lines specifying LVM devices will include line breaks where trash does not expect them to be. You can fix this by changing line 460 of /usr/lib/python2.5/site-packages/libtrash.py to include the -P option when spawning the df command, as shown below:

df_file=os.popen('df -P')
while True:

I also found an issue executing some trash commands when using bind mounts to mount filesystems in two locations. The commands would simply fail with ValueError: path is not a mount point, not informing which path is not a mount point or what you should do to fix the situation.


The trash project includes four commands: empty-trash, list-trash, restore-trash, and trash, the latter being the main command, with the others enabling full trashcan interaction from the command line. The only two commands that accept command-line parameters are empty-trash and trash. The empty-trash command accepts a single argument that specifies a cutoff for the number of days old that a trash item can be. For example, If you specify 7, then any items in your trashcan older than a week will be deleted. The trash command takes the files and directory names that you wish to put into your trashcan, and also accepts -d, -f, -i and -r options for compatibility with the rm(1) command. These last four options don't actually do anything with the trash command apart from make its invocation more familiar to users of the rm command.

Let's run through an example of how to use the trash commands:

$ mkdir trashdir1
$ date >trashdir1/dfa.txt
$ date >trashdir1/dfb.txt
$ list-trash
$ trash trashdir1
$ list-trash
2008-06-10 15:03:11 /home/ben/trashdir1
$ mkdir trashdir1
$ date >trashdir1/dfc.txt
$ trash trashdir1
$ list-trash
2008-06-10 15:04:01 /home/ben/trashdir1
2008-06-10 15:03:11 /home/ben/trashdir1
$ restore-trash
0 2008-06-10 15:04:01 /home/ben/trashdir1
1 2008-06-10 15:03:11 /home/ben/trashdir1
What file to restore [0..1]: 0
$ l trashdir1/
total 8.0K
-rw-rw-r-- 1 ben ben 29 2008-06-10 15:03 dfc.txt

As you can see, it is perfectly valid for multiple items in the trashcan to have the same file name and have been deleted from the same directory. Here I restored only the latest trashdir1 that was moved to the trashcan.

The restore-trash command must be executed in the directory of the trashed file. The above commands were all executed in my home directory; if I had been in /tmp and executed restore-trash, I would not have seen /home/ben/trashdir1 as a restore option. At times it might be misleading to execute restore-trash and be told that there are "No trashed files." Perhaps the developers should expand this message to inform you that there are "No trashed files for directory X" so that you have a hint that you should be in the directory that the file was deleted from before executing restore-trash. For scripting it might also be convenient to be able to use restore-trash with a path and have it restore the most recent file or directory with that name.

While the command-line options to the trash commands are currently fairly spartan, the ability to interact with the same trashcan that KDE 3 is using from the command line can assist folks getting into command-line interaction without stepping up to using the more permanent rm command right off the bat.


  • Shell & CLI
  • Tools & Utilities
Click Here!