Category Archives: Organization

Bash Directory Cleaner

There are times when one will have non-rotating log files in one’s code. The scripts keep generating more logs based on the date and the directories keep filling up. What is one supposed to do?

This is a quick Bash directory cleaner I use:

function usage() {
echo "Usage: $0 [d directory] [t time_in_days]"
exit 1
}

TIME=30
HOMEDIR=/home/zenoss
SCRIPTSDIR=${HOMEDIR}/scripts
LOGDIR=${SCRIPTSDIR}/log

REMOVEFILE=${LOGDIR}/findResults.$$.txt

while getopts d:t: flag
do
case "${flag}" in
d) DIRECTORY=${OPTARG};;
t) TIME=${OPTARG};;
[?]) usage;;
esac
done
shift $((OPTIND - 1))

if [ $# -ne 0 ]
then
usage
fi

if [ ! -d "$DIRECTORY" ]
then
echo "Directory specified was not a directory"
exit 2
fi

find ${DIRECTORY} -mtime +${TIME} > ${REMOVEFILE}

echo "Removing: "
cat ${REMOVEFILE}

for each in `cat ${REMOVEFILE}`
do
rm $each -f
done

rm ${REMOVEFILE}

This script allows one to chose a directory and how many days of logs to keep.

Here is an example usage:


0 12 * * 1 /home/user/scripts/bin/dirCleaner.sh -d/home/user/scripts/log -t31 >> /home/user/scripts/log/dirCleaner.`date +\%Y\%m\%d`.log 2>&1

Getting Started

When I first started working, my then employer sent most of its employees to a training called Mission Control.  I missed out on this training, but picked up a few things from my co-workers as they’d talk about it.  The main idea I came across was that one should list all of the one wants to do and then place them in three buckets.

  1. Doing now
  2. Not doing now
  3. Never doing

Each of my buckets in future posts.