#!/bin/sh # trm.sh - Safer 'rm' # 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" TESTING="Running in 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 OPERATOR="mv" # Can be overriden with -c option OPTLIST='cdg:stvx' # List of all legal command line options OPTIND=1 # getopts may have been previously used in this context 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} ;; # 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 # Process arguments shift $((OPTIND-1)) for arg in $* do # Symlinks require special care if [ -L $arg ] then # Bare symlink itelf REALPATH=$(readlink -f $(dirname $arg)) if [ ${REALPATH} = '/' ] then REALPATH="" fi REALPATH=${REALPATH}/$(basename $arg) # Process normal files and directories else REALPATH=$(readlink -f $arg) 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 ${TESTMODE} echo ${CMD} else mkdir -p ${DESDIR} eval ${CMD} fi done } trm $*