#!/bin/sh # trm.sh - Safer 'rm' And Backup Utility # Copyright (c) 2016 TundraWare Inc., Des Plaines, IL USA # All Rights Reserved # Permission granted for the free use of this program without restriction GRAVEYARD="${HOME}/.graveyard" INTERPROMPT="Do You Want To Remove(Copy):" TESTING="Test Mode ..." mkdir -p $GRAVEYARD # This function is called at the bottom of this file or it can # be embedded in a shell startup script. trm() { # Parse command line args OPTLIST='cdg:istvx' # List of all legal command line options OPTIND=1 # in case getopts was previously used in this context # Defaults INTERACTIVE="" OPERATOR="mv" # Can be overriden with -c option SERIALNO="Yes" # Generate serial numbers TESTMODE="" VERBOSE="" while getopts ${OPTLIST} opt do case $opt in # Copy, don't move, targets to graveyard c) OPERATOR="cp -pr" ;; # Empty the graveyard d) rm -rf ${VERBOSE} ${GRAVEYARD}/* ${GRAVEYARD}/.[-z]* ;; # Name your own graveyard g) GRAVEYARD=${OPTARG} ;; i) INTERACTIVE="Yes" ;; # Turn of serial number generation s) SERIALNO="" ;; # Don't do anything, just show what you would do t) TESTMODE=${TESTING} ;; # Be noisy v) VERBOSE="-v" ;; # Actually execute x) TESTMODE="" ;; *) echo "usage: trm.sh -${OPTLIST} [file|directory] ..." exit 1 ;; esac done # Notify if in test mode echo ${TESTMODE} # Process arguments shift $((OPTIND-1)) while [ $# -gt 0 ] do # Symlinks require special care if [ -L "$1" ] then # Bare symlink itelf REALPATH=$(readlink -f $(dirname "$1")) if [ "${REALPATH}" = '/' ] then REALPATH="" fi REALPATH="${REALPATH}"/$(basename "$1") # Process normal files and directories else REALPATH=$(readlink -f "$1") fi # See if we want serial numbers SERIAL="" if [ -n "${SERIALNO}" ] then SERIAL=".$(date +%Y%m%d%H%M%S)" fi SRCDIR=$(dirname "${REALPATH}") if [ "${SRCDIR}" = '/' ] then SRCDIR="" fi SRCFIL=$(basename "${REALPATH}") DESDIR="${GRAVEYARD}${SRCDIR}" DESFIL="${SRCFIL}${SERIAL}" CMD="${OPERATOR} $VERBOSE '${REALPATH}' '${DESDIR}/${DESFIL}'" if [ -n "${TESTMODE}" ] then echo ${CMD} else # Handle interactive requests if [ -n "${INTERACTIVE}" ] then echo -n "${INTERPROMPT} ${REALPATH}? " read doit if [ "${doit}" != "y" ] && [ "${doit}" != "Y" ] then shift continue fi fi mkdir -p "${DESDIR}" eval ${CMD} fi shift done } # Uncomment to invoke as a free standing utility trm "$@"