trash your-trash-to-get-rid-of
rather than rm [-f] your-trash-to-get-rid-of
#!/bin/sh
# trash - virtual trash-can to avoid accidental removing of files
WORKDIR="$HOME/.trash"
LOGFILE="$HOME/.trash.log"
ERR_NOGOODARG="\"$1\" is not an existing regular file or directory and not a known parameter."
ERR_NOWORKDIR="Script cannot work without a trash-directory (should be: \"$WORKDIR\")."
ERR_NOLOG="Logfile doesn't exist. It may not have been created yet (first use)."
SCRIPTBASENAME=`basename "$0"`
help()
{
echo "Usage : $SCRIPTBASENAME file-or-directory : drops file/directory to trash-can"
echo " $SCRIPTBASENAME -h|--help : displays this help screen"
echo " $SCRIPTBASENAME -s|--size : displays the size of the trash-can"
echo " $SCRIPTBASENAME -e|--empty : gets definitively rid of trash-can content"
echo " $SCRIPTBASENAME -l|--log : displays logfile"
echo " $SCRIPTBASENAME -tp|--trash-path : displays path of trash-can"
echo " $SCRIPTBASENAME -lp|--log-path : displays path of logfile"
echo ""
echo "(Your logfile is: $LOGFILE , your trash-directory is: $WORKDIR )"
}
echo_error()
{
echo "ERROR ! $1" >&2
echo "Type \"trash -h\" or \"trash --help\" for usage."
}
trash_size()
{
[ ! -d "$WORKDIR" ] && echo_error "$ERR_NOWORKDIR" && exit 1
cd "$WORKDIR"
echo "Total size of trash-can is `du -ms | sed 's/[ \.\t]//g'` Mb."
}
log()
{
[ -s "$LOGFILE" ] || touch $LOGFILE
echo "`date` : $@" >> $LOGFILE
}
# Parameters control
[ -z "$1" -o "$1" = "--help" -o "$1" = "-h" ] && help && exit 0
[ "$1" = "-s" -o "$1" = "--size" ] && trash_size && exit 0
[ "$1" = "-tp" -o "$1" = "--trash-path" ] && echo "$WORKDIR" && exit 0
[ "$1" = "-lp" -o "$1" = "--log-path" ] && echo "$LOGFILE" && exit 0
if [ "$1" = "-l" -o "$1" = "--log" ]; then
[ -s "$LOGFILE" ] && cat "$LOGFILE" && exit 0
echo_error "$ERR_NOLOG" && exit 1
fi
if [ "$1" = "-e" -o "$1" = "--empty" ]; then
echo "CAUTION: you are about to DEFINITIVELY get rid of all trash-can content."
echo -n ">> Are you really sure ? [y|N] "
read foo
if [ "$foo" = "y" -o "$foo" = "Y" ]; then
rm -fR "$WORKDIR/*"
echo "Trash-can is now empty."
log "Trash-can empty"
echo -n ">> Do you want to erase the logfile too ? [y|N] "
read foo2
if [ "$foo2" = "y" -o "$foo2" = "Y" ]; then
[ ! -s "$LOGFILE" ] && echo_error "ERR_NOLOG" && exit 1
mv "$LOGFILE" "${LOGFILE}.bak"
tail -n 1 "${LOGFILE}.bak" > "$LOGFILE"
mv "${LOGFILE}.bak" "$WORKDIR"
echo "Logfile has been dropped in trash-can (\"$WORKDIR\")."
log "Logfile $LOGFILE dropped in"
else
exit 1
fi
exit 0
else
exit 1
fi
fi
[ ! -f "$1" -a ! -d "$1" ] && echo_error "$ERR_NOGOODARG" && exit 1
# Proposer de créer la corbeille si elle n'existe pas déjà
if [ ! -d "$WORKDIR" ]; then
while true
do
echo "CAUTION: trash-can \"$WORKDIR\" (directory) is not existing."
echo -n ">> Should I create it for you ? [Y|n] "
read foo
[ "$foo" = "n" -o "$foo" = "N" ] && echo_error "$ERR_NOWORKDIR" && exit 1
[ "$foo" = "y" -o "$foo" = "Y" ] && break
done
mkdir -p "$WORKDIR"
log "Trash-can created as $WORKDIR"
fi
# Get rid of file/dir to trash-can
cp -a "$1" "$WORKDIR"
rm -fR "$1"
echo "\"$1\" has been dropped in trashcan (\"$WORKDIR\")."
log "$1 dropped in"
trash_size
exit 0
It looks like you're new here. If you want to get involved, click one of these buttons!