Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Tag Archives: file monitoring

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.


#!/bin/bash
# monitor file changes in a target directory

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

LOGDIR="$HOME/.log"

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

TARGET_DIR="$*"
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
do
echo "$line" >> $LOGNAME
done

exit 0

Advertisements
%d bloggers like this: