Thursday, July 10, 2008

Bringing the trashcan to the command line

Everything Linux and Open Source

Bringing the trashcan to the command line

June 17, 2008 (9:00:00 AM) - 3 weeks, 2 days ago

By: 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 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 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/ to include the -P option when spawning the df command, as shown below:

<div class="code"><br />       else:<br />          df_file=os.popen('df <b>-P</b>')<br />          while True:<br /></div></pre>

<p>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 <code>ValueError: path is not a mount point</code>, not informing which path is not a mount point or what you should do to fix the situation.</p>


<p>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 <code>-d</code>, <code>-f</code>, <code>-i</code> and <code>-r</code>
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.</p>

<p>Let's run through an example of how to use the trash commands:</p>

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

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.

Read in the original layout at:

1 comment:

Anonymous said...

Hi everyone! I'm originally from London but am living in Berlin at the moment.
Gotta like this place!

[url=]Barrater is my life[/url]