#!/bin/bash
SRC="/export/shared /export/beancounters/"
DEST_BASE="/export/trash"
for S in $SRC
do
DEST=$DEST_BASE/$(basename $S)
rm -rf ${DEST}/*
while read F
do
DIR=$(dirname "$F"); DIR=${DIR#$S}
if [[ ! -d $DIR ]]
then
mkdir -p \"${DEST}${DIR}\"
fi
mv \"$F\" \"${DEST}${DIR}\"
done < <(find $S -type f -mtime +15 | sort)
find $S -type d -empty -exec rmdir {} \;
done
Refactorings
No refactoring yet !
ghostdog74.livejournal.com
January 8, 2010, January 08, 2010 13:15, permalink
Since you are using bash, why not leave out basename and dirname and use bash's built in string methods.
#!/bin/bash
SRC="/export/shared /export/beancounters/"
DEST_BASE="/export/trash"
for S in $SRC
do
#DEST=$DEST_BASE/$(basename $S)
DEST="$DEST_BASE/${S##*/}" # added
rm -rf ${DEST}/*
while read -r F
do
#DIR=$(dirname "$F")
DIR=${F%/*} # added
DIR=${DIR#$S}
if [[ ! -d $DIR ]]
then
mkdir -p \"${DEST}${DIR}\"
fi
mv \"$F\" \"${DEST}${DIR}\"
done < <(find $S -type f -mtime +15 | sort)
find $S -type d -empty -exec rmdir {} \;
# if you have GNU find
# find $S -type d -empty -exec rmdir {} +;
done
I'm planning to run this snippet in a cron job on the 1st and 16th of each month. The idea is to periodically remove all the old stuff from a shared folder on our LAN. Do you see any issues with the code?