Domanda

I know standard Java still doesn't provide a standard way to put files in ecycle bin/trash instead of permanently deleting them. But thankfully there are solutions for WIndows (jna) and OSX ( com.apple.eio.FileManager) .

But is there anything for Linux Dekstop, how is recycle bin implemented on Linux, is it just a standard directory that I could move files to ?

È stato utile?

Soluzione

The two most common Linux Desktop Environments (KDE, Gnome) use the freedesktop.org trash specification. For the most common case, this consists of a directory structure under $XDG_DATA_HOME/Trash. if the environment variable is not set or is empty then $HOME/.local/share is used.

There are two directories under this one: files and info. files contains the trashed files, while info contains a desktop-style file which gives the information on the trashed file such as it's original name and location. Note that the file in files/ doesn't have to contain the original name of the file (e.g. in the case of filename collisions). There is a matching .trashinfo file in the info directory who's basename must match the entry in files/, so for example a file called foo in files/ would have a corresponding foo.trashinfo in info/.

The content of the .trashinfo file:

[Trash Info]
Path=<original path to file e.g. foo/bar/baz.txt>
DeletionDate=<Date file was deleted in RFC3339 format (YYYYMMDDTHH:MM:SS)>

It must have these lines, otherwise it is malformed.

A note in the spec states that:

When trashing a file or directory, the implementation MUST create the corresponding file in $trash/info first. When trashing a file or directory, the implementation MUST create thecorresponding file in $trash/info first. Moreover, it MUST try to do this in an atomic fashion, so that if two processes try trash files with the same filename this will result in two different trash files. On Unix-line systems this is done by generating a filename, and then opening with O_EXCL. If that succeeds the creation was atomic (at least on the same machine), if it fails you need to pick another filename.

So you have to perform the trashing operation in that manner.

This seems like a relatively simple exercise in Java, so once you understand how it's done, have at it!

In kde there is also the kfmclient CLI tool which can be used like so:

kfmclient move <file> trash:/

which does the job for you. I'm not aware of a gnome equivalent, though.

Altri suggerimenti

The trash folder in Linux is located at /home/username/.local/share/Trash. Try moving files to that folder.

You can use libtrash, most likely available from your package manager.

When configured properly, it adds recycle bin functionality to all programs and commands.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top