Bash: Monitor file addition/deletion in a directory

Silly little script to monitor changes in a directory (new or removed files)

Takes the destination dir to monitor from stdin

One day I'll learn how to use inotify.

#!/bin/bash
 
# Monitor a directory for file changes
# (add or remove) as requested
 
touch /tmp/testdirb.$$
while true
do
  find $1 -print > /tmp/testdira.$$
  diff /tmp/testdira.$$ /tmp/testdirb.$$ > /tmp/dirdiff 
  if [ -s /tmp/dirdiff ]
    then
      sed -i s/"<"/"New files"/ /tmp/dirdiff
      sed -i s/">"/"Removed files"/ /tmp/dirdiff
      /usr/bin/mail -s "Files changed in $1" "you@example.com" <<EOF
There have been file changes in $1 since the last hour.
`cat /tmp/dirdiff`
EOF
  fi
 
  cp /tmp/testdira.$$ /tmp/testdirb.$$
  sleep 3600
done

Cool Bananas, thanks for

Cool Bananas, thanks for sharing.