#!/bin/bash
# recycle bin configuration. all the deleted files will be moved to an
# archive and viewed/recovered/purged.
# the real 'rm' command
bin_rm=/bin/rm
# where archiving the files
Archive=~/.rm_saved.tar
# you may prefer something like :
# Archive=/var/trash/$USER/saved_file.tar
# (with write access permission on the directory)
# global variables for the options
Opt_recursive=0
Opt_no_secure=0
Opt_restore=0
Opt_rm=""
# function for archiving a file or a directory
save_file() {
if [ $Opt_no_secure -ne 1 ] ; then
# set date/time of deletion
touch "$1" > /dev/null 2>&1
if [ -f $Archive ] ; then
tar --delete -f "$Archive" "$1" > /dev/null 2>&1
tar -rf "$Archive" "$1" > /dev/null 2>&1
else
tar -cf "$Archive" "$1" > /dev/null 2>&1
# r/w access only for the user
chmod 600 "$Archive"
fi
fi
}
# function for restoring file or directory
restore_file () {
if [ -f $Archive ] ; then
tar -xf "$Archive" "$1" > /dev/null 2>&1
tar --delete -f "$Archive" "$1" > /dev/null 2>&1
fi
}
# reading the command-line args
while getopts "dfirRvns-:" opt ; do
case $opt in
d ) Opt_rm="$Opt_rm -d" ;;
f ) Opt_rm="$Opt_rm -f" ;;
i ) Opt_rm="$Opt_rm -i" ;;
r | R ) Opt_recursive=1
Opt_rm="$Opt_rm -r" ;;
v ) Opt_rm="$Opt_rm -v" ;;
n ) Opt_no_secure=1 ;;
s ) Option_restore=1 ;;
- ) case $OPTARG in
directory ) Opt_rm="$Opt_rm -d" ;;
force ) Opt_rm="$Opt_rm -f" ;;
interactive ) Opt_rm="$Opt_rm -i" ;;
recursive ) Opt_recursive=1
Opt_rm="$Opt_rm -r" ;;
help ) $bin_rm --help
echo "(rm_secure)"
echo " -n, --nosecure delete without backup"
echo " --viewtrash list the saved files"
echo " --emptytrash erase the saved files"
echo " -s, --restore restore the specified files"
exit 0 ;;
version ) $bin_rm --version
echo "(rm_secure 1.0)"
exit 0 ;;
verbose ) Opt_rm="$Opt_rm -v" ;;
viewtrash ) if [ -f $Archive.gz ] ; then
tar -tvzf $Archive.gz
fi
exit 0 ;;
nosecure ) Opt_no_secure=1 ;;
emptytrash ) if [ -f $Archive.gz ] ; then
$bin_rm $Archive.gz
fi
exit 0 ;;
restore ) Opt_restore=1 ;;
* ) ;;
esac ;;
? ) ;;
esac
done
shift $(($OPTIND - 1))
gunzip $Archive.gz > /dev/null 2>&1
# restoration ?
if [ $Opt_restore -ne 0 ] ; then
while [ -n "$1" ] ; do
restore_file "$1"
shift
done
exit 0
else
while [ -n "$1" ] ; do
if [ -d "$1" ] ; then
# the directories are archived only with
# the -r option
if [ $Opt_recursive -ne 0 ] ; then
save_file "$1"
fi
$bin_rm $Opt_rm $1
elif [ -e "$1" ] ; then
# existing file
save_file "$1"
$bin_rm $Opt_rm $1
else
# let 'rm' give his error message
$bin_rm $1
fi
shift
done
fi
nice gzip $Archive > /dev/null 2>&1 &
--
--
Dileep M Kumar <[EMAIL PROTECTED]> http://www.kumarayil.org/
Debian GNU/Linux 2.2.17 (Potato) Mutt 1.2.5i (2000-07-28) || VIM 5.6
--
----------------------------------------------
LIH is all for free speech. But it was created
for a purpose. Violations of the rules of
this list will result in stern action.