cleanup handling of file- and directory names with embedded spaces
1 parent bcd7215 commit 779757d3d4ea288a1d3303efab4e7482f3119a27
@tundra tundra authored on 27 Oct 2016
Showing 2 changed files
View
4
TODO.txt
- Handle filenames and destinations with spaces in them
- Add environment variable to override defaults
- Make serial number postfix consistent across dirs and files
- Add -h help and usage screen
 
Doc Notes
you can clean multiple graveyards with:
 
/trm.sh -g ./foo -d -g ./baz -d
 
- Note difference between -vd and -dv
View
132
trm.sh
#!/bin/sh
# trm.sh - Safer 'rm'
# 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
 
 
# Process arguments
 
shift $((OPTIND-1))
for arg in $*
while [ $# -gt 0 ]
do
 
# Symlinks require special care
if [ -L $arg ]
# Symlinks require special care
if [ -L "$1" ]
then
 
# Bare symlink itelf
REALPATH=$(readlink -f $(dirname "$1"))
if [ "${REALPATH}" = '/' ]
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)
REALPATH=""
fi
 
# See if we want serial numbers
SERIAL=""
if [ -n "${SERIALNO}" ]
then
SERIAL=".$(date +%Y%m%d%H%M%S)"
fi
REALPATH="${REALPATH}"/$(basename "$1")
 
SRCDIR=$(dirname $REALPATH)
if [ ${SRCDIR} = '/' ]
then
SRCDIR=""
fi
SRCFIL=$(basename $REALPATH)
DESDIR=${GRAVEYARD}${SRCDIR}
DESFIL="${SRCFIL}${SERIAL}"
# Process normal files and directories
else
REALPATH=$(readlink -f "$1")
fi
 
CMD="${OPERATOR} $VERBOSE '${REALPATH}' '${DESDIR}/${DESFIL}'"
if [ -n "${TESTMODE}" ]
then
echo ${TESTMODE}
echo ${CMD}
else
mkdir -p ${DESDIR}
eval ${CMD}
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
 
shift
 
done
 
}
 
trm $*
# Uncomment to invoke as a free standing utility
 
# trm "$@"