I have a directory called /home/user/local
. Every two minutes or so, a new file is dumped into this directory. I need to check this directory every 2 minutes to see
#! /usr/bin/env bash
FILELIST=/tmp/filelist
MONITOR_DIR=/home/usr/local
[[ -f ${FILELIST} ]] || ls ${MONITOR_DIR} > ${FILELIST}
while : ; do
cur_files=$(ls ${MONITOR_DIR})
diff <(cat ${FILELIST}) <(echo $cur_files) || \
{ echo "Alert: ${MONITOR_DIR} changed" ;
# Overwrite file list with the new one.
echo $cur_files > ${FILELIST} ;
}
echo "Waiting for changes."
sleep $(expr 60 \* 2)
done
a quick & dirty way. :) It'll monitor the directory for changes, not only when there's new file dumped in, but also if some file is missing/deleted.
File list is stored in variable $cur_files
.