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 🙂

They say falling for a sin is a sign of weakness. Is it though? I see people waving white flags and others embracing them with joy.

I honestly don’t know what’s worse: sins we love or stuff we love to hate? Or is the latter a sin, too?

I often see people acting weird, as if generally admitted moral code or common sense are absent from their motives. But how is common sense established? Are sins just a religion-sourced know-what-to-avoid guidelines? Well, i am not sure. In times of great sorrow and crisis only few have the clarity of mind to challenge the crowds. Hell, i am part of the rest. And I definitely hate to love to hate. Is that a sin, too?