Using .Trash with shell applications
How many times you did the following?
~$ rm -rf some/stu[Tab] ~$ rm -rf some/stuff *
(notice the space before the star)
It turned out, that there is only one stuff* file, and before you realized what is happening half of your home directory was gone.
You’re not the only one. Been there, done that… Oh, how I wished that Nautilus ~/.Trash was supported under the shell.
And it is! The problem of recovering mistakenly deleted files is so common, that there is a solution already. It’s called libtrash, and works by utilizing the neat LD_PRELOAD trick. Preloaded library intercepts all system calls that causes removal of files and moves them to the user trashcan.
Installation and setup.
$ sudo apt-get install libtrash
The default directory for the trascan is ~/Trash. I want the files to be put in GNOME trashcan, so:
$ sudo nano /etc/libtrash.conf
and change
TRASH_CAN = Trash
to
TRASH_CAN = .Trash
Second part is enabling the library via LD_PRELOAD. You might just put
export LD_PRELOAD=/usr/lib/libtrash/libtrash.so.2.4
to your ~/.bash_profile, but that would not work for files deleted by bash internal commands, so you need to add the following snippet to your ~/.bash_profile (it’s from /usr/share/doc/libtrash/examples/example.bash_profile ):
LIBTRASH=/usr/lib/libtrash/libtrash.so
LIBTRASH_dest=$LIBTRASH
if test -L $LIBTRASH ; then
LIBTRASH_dest="$(dirname $LIBTRASH)/$(ls -l "$LIBTRASH" | \
sed -e 's/^.\+ -> //')"
fi
if test x${LD_PRELOAD+set} = x -a -f "$LIBTRASH_dest" ; then
LD_PRELOAD="$LIBTRASH"
export LD_PRELOAD
if test x$0 = x-bash ; then
exec bash ${1+"$@"}
else
exec $0 ${1+"$@"}
fi
fi
Have fun and never loose your precious files again.
No comments
Jump to comment form | comments rss | trackback uri