You are not logged in.

#1 2007-03-19 05:48:03

bsdson.tw
Member
From: Taiwan
Registered: 2005-05-06
Posts: 161

A command-line implement compliant to freedesktop's trash standard!

Hi,

I'm using Xfce4 as my desktop environment.
And if I use Thunar to remove files, it will put these files into the trash can.

Usually I use "command line interface", so I wonder if there is a "remove" command that uses Xfce4's trash service?
If no, is it possible to create one?

Best regards,
bsdson.tw

Last edited by bsdson.tw (2007-03-26 04:01:08)

Offline

#2 2007-03-19 08:26:43

mclang
Member
From: Finland
Registered: 2005-10-24
Posts: 79

Re: A command-line implement compliant to freedesktop's trash standard!

Could you alias the normal rm command to move files to xfce4 trash instead of deleting? I don't know the right location though...


Duettaeánn aef cirrán Cáerme Gláeddyv. Yn á esseáth.

Offline

#3 2007-03-19 09:23:29

Crooksey
Member
From: UK ~
Registered: 2006-08-14
Posts: 415
Website

Re: A command-line implement compliant to freedesktop's trash standard!

I can write you a perl script that will use the command "xfcerm" that will place the file in the /home/username/.trash folder.

Or wherever xfce places files.


Arch Linux since 2006
Python Web Developer + Sys Admin (Gentoo/BSD)

Offline

#4 2007-03-19 10:11:25

psyBSD
Member
From: The Netherlands
Registered: 2007-03-01
Posts: 25

Re: A command-line implement compliant to freedesktop's trash standard!

Crooksey wrote:

I can write you a perl script that will use the command "xfcerm" that will place the file in the /home/username/.trash folder.

Or wherever xfce places files.

It does so where the freedesktop.org standards specify it should do it:

~/.local/share/Trash/files/

xfcerm, or how you call it (please not like that) should should implement the trash spec (it should know how to handle multiple files with the same filename). so it is not trivial for a cli-app.

Last edited by psyBSD (2007-03-19 10:12:05)

Offline

#5 2007-03-19 15:18:55

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: A command-line implement compliant to freedesktop's trash standard!

I'm looking at the freedesktop.org trash specification right now; it doesn't look too difficult. I'll come up with a quickie Python implementation and post it today or tomorrow.

Offline

#6 2007-03-19 21:23:01

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: A command-line implement compliant to freedesktop's trash standard!

It's probably best not to call it "rm" though. Not to big a problem when someone else uses your system, but maybe trouble when you use rm elsewhere and expect it not to blast the file. Perhaps "trash" or something like "rb" for rubbish would be better. Or "to" for "throw out" would be easier to type.

Offline

#7 2007-03-20 22:12:30

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: A command-line implement compliant to freedesktop's trash standard!

Update: I've been working on that Python script, it's around half done. There have been a few (minor) speed bumps, and I've been busier than I expected, so I may not post it for another couple days.

Offline

#8 2007-03-21 00:54:28

barebones
Member
Registered: 2006-04-30
Posts: 235

Re: A command-line implement compliant to freedesktop's trash standard!

Is this script going to be xfce dependent? Something like this sounds handy even for a system that is comand-line only.

Offline

#9 2007-03-21 01:08:00

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: A command-line implement compliant to freedesktop's trash standard!

barebones wrote:

Is this script going to be xfce dependent? Something like this sounds handy even for a system that is comand-line only.

No, it won't depend on anything outside of the Python standard library. The first version will probably only support trashing files, but it shouldn't be much more work to add list and un-trash commands.

Offline

#10 2007-03-22 02:40:49

bsdson.tw
Member
From: Taiwan
Registered: 2005-05-06
Posts: 161

Re: A command-line implement compliant to freedesktop's trash standard!

Here is my original "rm" implementation with trash-can support, hope that helps:

#!/bin/sh

# rm replacement with un-rm ability
# created by Henry

# prepare trashCan
trashCan=/home/trashCan/`date +%y%m%d/%H%M%S`
mkdir -p $trashCan

#
echo "DEL_BASE=$PWD" >> $trashCan/.files.del
echo "DEL_FILES=($*)" >> $trashCan/.files.del

# do rm
for file; do
        mv $file $trashCan
done

There are some things that need to be modified:
1. the path to the trash-can, I use a semi-unique path to avoid conflictions
2. the target filename, now need to be considered.
3. the info file format
...

BR,
bsdson.tw

Last edited by bsdson.tw (2007-03-22 02:41:45)

Offline

#11 2007-03-22 16:55:56

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: A command-line implement compliant to freedesktop's trash standard!

My Python implementation is pretty much finished (minus listing and un-trashing, I'll add that later). I've done some testing, but there are probably still bugs in situations I didn't test.

I'm fighting my ISP's web site (why do the biggest companies hire the worst web designers?) to upload it to my web space. I'll post a download link when I can.


EDIT: Okay, here's the PKGBUILD, and the unpackaged script.

If it throws an exception at you, that's a bug, so report it.

EDIT 2: I released version 0.1.1. The only change is a re-licensing to GPL (instead of public domain). I've also uploaded it to the AUR.

Last edited by skymt (2007-03-22 20:05:54)

Offline

#12 2007-03-26 03:56:01

bsdson.tw
Member
From: Taiwan
Registered: 2005-05-06
Posts: 161

Re: A command-line implement compliant to freedesktop's trash standard!

Thank you very much, skymt.
I'll try it out!

Sorry for late replying.

Offline

#13 2007-03-26 09:18:43

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: A command-line implement compliant to freedesktop's trash standard!

http://directory.fsf.org/libtrash.html

libtrash might be fun, and it'll act underneath rm and other standard apps. May not be fd.o compatible, but im sure the trash directory is configurable at least.

James

Offline

#14 2007-03-26 10:52:06

Aziere
Member
Registered: 2007-03-16
Posts: 23

Re: A command-line implement compliant to freedesktop's trash standard!

I wrote a little script for this. Only problem is that it only accepts full paths. I'll fix that later though, pretty functional as it is. Warning though: I wont take any responsibility if anything goes wrong.

#!/bin/bash
x="$1"; y="$2"
if [ "$x" != "-r" ]; then trash="$x"; else trash="$y"; fi
if [ "$x" == "-r" ]; then echo "rm -r $trash" >> ~/.rmtrash.sh; fi
if [ "$x" != "-r" ] && [ "$x" != "-c" ] && [ "$x" != "-e" ] && [ "$x" != "" ]; then echo "rm $trash" >> ~/.rmtrash.sh; fi
if [ "$x" == "-c" ]; then echo "" > ~/.rmtrash.sh; fi
if [ "$x" == "-e" ]; then sh ~/.rmtrash.sh; echo "" > ~/.rmtrash.sh; fi

Works like this. Just run the script along with the path as an argument and it will put it in a temporary script that stores it for later removal. If you add -r before the path, it will remove the contents of a folder as well. If you add -c as an argument, it will clear the trash without removing the files (not tested, but should work). If you add -e as an argument it will remove all files in the trash.

I prefer this way of handling trash than the regular 'move files to a folder' way, because this way it doesn't require any work from the harddrives. And it's also more friendly if you can't store everything in the trash because of partition sizes.

As for automatic trash removal, just add it to a cron with -e.

P.S: I know it's a really ugly script. tongue

Edit: Forgot to mention, it's user dependent.

Last edited by Aziere (2007-03-26 10:56:05)

Offline

#15 2007-09-07 10:29:03

bsdson.tw
Member
From: Taiwan
Registered: 2005-05-06
Posts: 161

Re: A command-line implement compliant to freedesktop's trash standard!

Here is my simple bash implementation,
which only do some simple error check and no auto recovery...

Hope this helps.

#!/bin/bash

# http://www.ramendik.ru/docs/trashspec.html

# home_trash_can=$XDG_DATA_HOME/Trash
# $home_trash_can/info
# $home_trash_can/files
# create XXX.trashinfo first
# [Trash Info]
# Path=$org_location/$org_filename # absolute path name
# DeletionDate=YYYY-MM-DDThh:mm:ss

# requirement:
# realpath

# -------------------------------------------------------

# init
deletion_date=$(date +%Y-%m-%dT%H:%M:%S)
home_trash_can=$XDG_DATA_HOME/Trash

if [ ! -d "$home_trash_can/info" -o ! -r "$home_trash_can/info" -o ! -w "$home_trash_can/info" ]; then
        # 錯誤處理
        echo "Something wrong with directory $home_trash_can/info..."
        exit 4
fi

if [ ! -d "$home_trash_can/files" -o ! -r "$home_trash_can/files" -o ! -w "$home_trash_can/files" ]; then
        # 錯誤處理
        echo "Something wrong with directory $home_trash_can/files..."
        exit 6
fi

# parse arguments
# nop

#
for file in "$@"; do

        #
        abs_path_name=$(realpath -s $file)
        org_location=$(dirname $abs_path_name)
        org_filename=$(basename $abs_path_name)
        if [ ! -e $file -o ! -x $org_location -o ! -w $org_location ]; then
                echo "Something wrong with file $file"
                echo "abs_path_name=$abs_path_name"
                echo
                exit 8
        fi

        #
        ext=""
        while [ -e "$home_trash_can/info/$org_filename$ext.trashinfo" ]; do
                #sleep .$RANDOM
                ext=$(($ext + 1))
        done
        info="$home_trash_can/info/$org_filename$ext.trashinfo"
        touch $info

        echo "[Trash Info]" > $info
        echo "Path=$abs_path_name" >> $info
        echo "DeletionDate=$deletion_date" >> $info

        #
        mv $file $home_trash_can/files/$org_filename$ext&
done

Offline

Board footer

Powered by FluxBB