Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Folder Monitoring Script using Inotifywait

Though it is not failsafe nor flawless, but currently it is working great for me. Here’s a script that enables to monitor the events from a specified folder/directory.

First install the inotify-tools:

apt-get install inotify-tools

Here’s my current code, feel free to enhance.

# monitor file changes in a target directory

if [ "$*" = "" ]; then
 echo "File Monitoring"
 echo "USAGE: $0 <target directory>" | sed "s/.\///g"
 echo ""
 exit 0


if [ ! -d $LOGDIR ]; then
 mkdir -p $LOGDIR

LOGNAME="$LOGDIR/`date -I`.log"

inotifywait -m -r --format '%T %e %w%f' --timefmt '%F %T' -e modify -e move -e create -e delete $TARGET_DIR | while read line
echo "$line" >> $LOGNAME

exit 0


6 responses to “Folder Monitoring Script using Inotifywait

  1. Gabriel Mercado March 13, 2012 at 8:41 pm

    hi ron i was looking for a solution like this. Can I tell you what my issue is and tell me if applicable?

    see, I want to run a caching solution called Boost for my Drupal site. What it does is make an html copy of each visited page, drastically increasing the speed and limiting the need to access the db, making the server’s life easier.

    the prob is the last time i tried to use it, the server ran out of inodes and crashed. So I was looking for a way to limit the amt of files and / or diskspace of the cache dir, and make it rm oldest files (or maybe largest files?)

    any thoughts would be appreciated 🙂

  2. Susan Lloyd October 31, 2012 at 9:25 pm

    Nice piece of script, but you didn’t say exactly what your script does other than it monitors your folder. So, does it monitor it for new files, delete or what? I would like to know because it looks quite interesting.

  3. Slim March 11, 2013 at 6:31 pm

    the script no detection when downloading files in a target directory.

    please find me a solution

  4. Pingback: OSX: Execute whack book when record appears in folder - Ziff Gyan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: