Watching directories for file changes with inotifywait.
I was in need for a quick script that would monitor a certain directory for file changes.
The case is a quite easy one: I own a directory where the results of certain simulations are stored. As I wanted to know when a new simulation begins and what is the number of simulations already finished, I decided to take a look at the way I could handle with this new challenge.
The first attempt was an easy one, using time delays and commands like $ ls -1 | wc -l and of course $ sleep. However this was quite annoying as i kept getting multiple indications for the same number of simulation results. Then, I discovered a package named inotify-tools. inotify-tools contains several useful commands that take advantage of the inotify events dispatched by the respective Linux kernel subsystem. I am not going to list the uses of the commands contained in inotify-tools, further information can be found in the project homepage, here. Be sure to check it, it is really valuable 🙂
Anyway, a simple yet effective command that eventually did the trick for me was $ inotifywait. As stated in its manpage:
inotifywait efficiently waits for changes to files using Linux’s inotify(7) interface. It is suitable for waiting for changes to files from shell scripts. It can either exit once an event occurs, or continually execute and output events as they occur.
So, inotifywait watches a directory for file changes and fires certain events, such as create, delete, modify etc. PLUS it is really simple to use. In my case, where i wanted to check for file changes in directory $WATCH_PATH and display the file count, each time a file is created, it was fairly simple 🙂
#!/bin/bash
WATCH_PATH=”../results/”
FILENAMES=”sim_*”
WATCH_COUNT=0
COUNTER=0;
declare -i WATCH_COUNT;
declare -i COUNTER;
inotifywait -m –format ‘%f’ -e create $WATCH_PATH | while read FILE
do
WATCH_COUNT=$WATCH_COUNT+1;
if [ $WATCH_COUNT -eq 1 ]; then
echo -ne “Files found: “
fi
COUNTER=$(ls -1 $WATCH_PATH/$FILENAMES | wc -l);
if [ $COUNTER -eq 1 ]; then
echo -ne “\nStarted new simulation. \nFiles found: “
fi
echo -n “$COUNTER “;
done
The switches in inotifywait are quite standard and are well described in the manpage. -m states that the command should monitor the directory indefinitely (telling it not to exit upon a single file change), –format ‘%f’ describes the formatting of the output (piped to the while statement) while -e create indicates that only CREATE events are monitored. The output is the one attached 🙂
=-=-=-=-=
Powered by Blogilo

Very helpful. I’m a bit of a novice to bash, and I always wondered how to get the output of inotifywait when using the -m option. Now I know! Thanks much!!
🙂
Nice to know it helped you!
Thanks for the quick tutorial, it was very helpful. Perhaps an article on how to add it to startup or possibly as a service in init.d.
For the moment I created the shell script as described in the article, then added the line exec count.sh in my /etc/rc.local file