Linux.com

Feature: Shell & CLI

Bringing the trashcan to the command line

By Ben Martin on June 17, 2008 (9:00:00 AM)

Share    Print    Comments   

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.

Installation

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:

else: 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.

Usage

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.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   

Comments

on Bringing the trashcan to the command line

Note: Comments are owned by the poster. We are not responsible for their content.

Bringing the trashcan to the command line

Posted by: Anonymous [ip: 192.117.111.61] on June 17, 2008 12:57 PM
Unless rm is aliased to trash, I don't see this really helping at all. When you delete a file, it is because you think that you really want it gone.

Also, I really wish that the Gnome folks would get on the GUT (grand unified trash) bandwagon already.

#

Re: Bringing the trashcan to the command line

Posted by: Vassil Dichev on June 17, 2008 01:01 PM
I actually like the libtrash library, which intercepts what rm is doing. Maybe the next Linux.com article will be on that? ;-)

#

Re: Bringing the trashcan to the command line

Posted by: Anonymous [ip: 76.177.52.225] on June 17, 2008 06:53 PM
We did already. GVFS/GIO supports the FreeDesktop.Org Trash Specification, and is theoretically usable with this tool. Also, gvfs-trash will send a file to the Trash.

#

Re: Bringing the trashcan to the command line

Posted by: Vasily Tarasov on June 17, 2008 06:55 PM
I personally don't think so. I use bash autocompletion a lot and sometimes it happens that I remove a wrong file. The only solution in this case on ext3 filesystem (if you don't have a copy :)) is to grep your hard drive and pray that the information was not overwritten. So trash is a very nice tool for me. I just need to use to use it instead rm. ;) Also aliasing rm to trash will force a lot of scripts to use it, which is not always what you want.

An interesting question for me is which tool is faster? Have anybody measured trash vs rm on a large directory? As trash is written in python it is inherently slower plus it needs to do some accounting, but on the other hand it does not use sys_unlink(), which can be slower than sys_rename()...

#

Bringing the trashcan to the command line

Posted by: Anonymous [ip: 159.238.13.56] on June 17, 2008 03:25 PM
This is a good first step. As the concept is refined, I hope we can see this as a configurable action for rm and any other programmatic file removal.

#

Bringing the trashcan to the command line

Posted by: Anonymous [ip: 151.66.137.166] on June 21, 2008 05:57 PM
I'm the writer of the trash command.
I know that the user interface is not perfect.
If anyone has some suggestion please feel free to send to me, or to my email address or trough the sourceforge tracker.
Tracker: http://sourceforge.net/tracker/?group_id=87144
my email address: andrea.francia@REMOVE-FROM-HERE.ohoihihoihoih.TO-HERE.gmx.it

#

Bringing the trashcan to the command line

Posted by: Anonymous [ip: 81.32.75.8] on July 16, 2008 07:50 PM
> When you delete a file, it is because you think that you really want it gone.

You make mistakes, like everyone.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya