Monitor a Directory for File Changes with inotifywait
Sometimes there may be a need to monitor and be alerted when any new files have been added in a particular directory.
One of the quickest way to do that is to use inotifywait
. You can find it in the inotify-tools
package.
If you don’t have it yet, you can install it with apt-get install inotify-tools
if you’re on Debian/Ubuntu.
Create a nice looking bash script. Maybe something called watcher.sh
, copy the following script and fire away!
#!/bin/bash
inotifywait -m /path/to/directory/to/observe -e create -e moved_to |
while read path action file; do
curl -X POST --data-urlencode 'payload={"text": "'"$file"' is uploaded to '"$path"' on '"$HOSTNAME"'."}' https://hooks.slack.com/services/secret
echo "The file '$file' appeared in directory '$path' via '$action'"
done
This particular snippet will send out a Slack alert whenever a directory has new files added.