#!/bin/bash # This script performs a "soft delete" of given files. Any files # specified in the arguments to the script will be compressed with # the tar command and moved into the ~/TRASH directory. Any files # in the TRASH directory that haven't been touched in 2 days will # be permanently deleted. # Create the TRASH directory if it does not exist. trash=~/TRASH if [ ! -e $trash ]; then mkdir $trash elif [ ! -d $trash ]; then echo "$0: error: $trash is not a directory"; exit 1 fi # For each argument, check it exists, then tar it, move it, and # delete the original. while [ $# -gt 0 ]; do if [ ! -e $1 ]; then echo "$0: error: tried to delete file that does not exist: $1" shift continue fi tarname="$1.tar" tar -cf "$tarname" "$1" mv "$tarname" $trash rm -rf "$1" shift done # Go through the ~/TRASH directory and remove files older than # 2 days. now=`date +%s` cd $trash for f in `ls`; do fileage=$(stat -c '%Y' "$f") if [ $((now-fileage)) -gt $((60*60*24*2)) ]; then rm -rf "$f" fi done