Trash, like safeRM but different…

September 9, 2008

I had a bad morning today. I was working on a script that parses out log files and ran it from my desktop. It’s supposed to change to a specified directory and check for, then delete, any logs that are over a month old. Unfortunately I didn’t have that directory on my machine, and I didn’t exit the script after a failed directory change. Poof, there go my desktop documents.

As an impulse reaction I looked for something to replace ‘rm’, and found safeRM. I was not impressed. Why have a ‘dustbin’ folder if we already have that built into the desktop environment, i.e. the Trash can?

Here is my own brief script that (on openSuSE) moves things to the trash. Note that it is not set up for moving directories and does not emulate ‘rm’ entirely.

#!/bin/bash

TRASHDIR=~/.local/share/Trash
X=1
NAME=$(basename $1)
while [ -f $TRASHDIR/$NAME ]; do
        NAME=${NAME}_$X
        X=$(($X + 1))
done
mv $NAME $TRASHDIR/files/
echo "[Trash Info]" > $TRASHDIR/info/$NAME.trashinfo
echo "Path=$PWD/$1" >> $TRASHDIR/info/$NAME.trashinfo
echo "DeletionDate=$(date +%Y-%m-%dT%H:%M:%S)" >> $TRASHDIR/info/$NAME.trashinfo

By the time I got this far I decided that this wasn’t worth pursuing. I’ll just try to be more careful and will absolutely run backups. I thought I’d publish it anyway just in case someone wants a safeRM alternative or to be able to put things in the trash from the command line.

Categories: Geek
Tags: ,

Leave A Comment

Your email will not be published.