You may replace the secureMove function with a simple move statement (secureMove increments a counter in the filename should the same file exist in the destination folder already).
#!/usr/bin/bash
#Version: v0.2
#Date: 21th of October, 2009
LOG=1
DEBUG=0
WARN=1
LOGDIR="/opt/logs"
DESTDIR="/export/home/li"
debug()
{
if [ "$DEBUG" = 1 ] ; then
/usr/bin/logger -p local7.debug "FileMove:$1"
fi
}
log()
{
if [ "$LOG" = 1 ] ; then
/usr/bin/logger -p local7.notice "FileMove:$1"
fi
}
warn()
{
if [ "$WARN" = 1 ] ; then
/usr/bin/logger -p local7.warn "FileMove:$1"
fi
}
echo
detect_os()
{
OS=`uname`
case $OS in
Linux) debug "Detecting OS... Linux";;
SunOS) debug "Detecting OS... Solaris";;
*) log "Detecting OS... Unknown OS! Only Linux and Solaris are supported so far. Exiting..."; exit 0;;
esac
}
define_procs()
{
case $OS in
Linux)
ECHO=/bin/echo
CAT=/bin/cat
EGREP=/bin/egrep
GREP=/bin/grep
SED=/bin/sed
TR=/usr/bin/tr
AWK=/bin/awk
FIND=/usr/bin/find
XARGS=/usr/bin/xargs
BC=/usr/bin/bc
WC=/usr/bin/wc
;;
SunOS)
ECHO=/usr/bin/echo
CAT=/usr/bin/cat
EGREP=/usr/bin/egrep
GREP=/usr/bin/grep
SED=/usr/bin/sed
TR=/usr/bin/tr
CHOWN=/usr/bin/chown
MV=/usr/bin/mv
if [ -f /usr/xpg4/bin/awk ]
then
AWK=/usr/xpg4/bin/awk
else
AWK=/usr/bin/awk
fi
FIND=/usr/bin/find
XARGS=/usr/bin/xargs
BC=/usr/bin/bc
WC=/usr/bin/wc
EXPR=/usr/bin/expr
CP=/usr/bin/cp
;;
esac
}
secureMove(){
FILE="$1"
DESTFILE=$FILE
COUNTER=`$ECHO $FILE | sed -e 's/\.log$//;s/.*-//g'`
debug "Current counter value is $COUNTER"
while [ -f "${DESTDIR}/${DESTFILE}" -o -f "${DESTDIR}/retrieved.${DESTFILE}" ]
do
debug "File ${DESTDIR}/(retrieved.)${DESTFILE} exists already. Incrementing counter..."
COUNTER=`$EXPR $COUNTER + 1`;
DESTFILE=`$ECHO $DESTFILE | $SED -e "s/[^-][0-9]*\.log$/${COUNTER}\.log/"`
debug "...new filename $DESTFILE"
done
$CHOWN li:li "${FILE}"
$MV "${FILE}" "${DESTDIR}/${DESTFILE}"
if [ $? = 0 ]; then
log "$FILE moved SUCCESSfully to ${DESTDIR}/${DESTFILE}";
else
warn "FAILED to move file $FILE!!";
fi
}
processFile(){
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
warn " $FILE does not exists"
exit 1
elif [ ! -r $FILE ]; then
warn " Unable to read $FILE"
exit 2
fi
secureMove $FILE
}
processDir(){
DIR=$1
filecount=`$FIND ${DIR} -name "*ACCOUNTING*\-*\-*.log" -print | $WC -l | $TR -s '[:space:]' -`
log "moving ${filecount} accounting files from ${DIR} to ${DESTDIR}"
cd $LOGDIR
for FILE in `/usr/bin/ls -1 *ACCOUNTING*\-*\-*.log`
do
processFile $FILE
done
}
detect_os
define_procs
processDir $LOGDIR