All Linux HowTo's Scripting HowTo's

Watch a directory for changes with inotify

The following is a quick and simple way to watch a directory for changes and then report any such changes into a file. For this example the watched directory is

/var/www/

and the log file location is

/srv/log.txt
apt-get install inotify-tools
apt-get install screen

screen -S "inotify script"

inotifywait -m -r /var/www/ --timefmt %d-%m-%Y --format '%T%w%f%e' >> /srv/log.txt

You can press ctrl a d to escape from this session, and then you can check your log file using the following

 cat /srv/log.txt

————–
Watched directory = /var/www
Output log file = /srv/log.txt

Log output
%T = Logging the date/time specified under the –timefmt switch
%w = Outputs the name of the file that was changed (for this example is the name of the directory)
%f = Outputs the name of the file that caused an event in the watched directory
%e = event that occurred

Similar Posts: