Inotify is a subsystem of the Linux kernl monitoring file systems and reporting changes to user space appliactions. In a shell like Bash, the tool inotifywait can be used to call scripts or other actions based on Inotify events.

The Kernel subsystem Inotfiy monitors file systems for events like access to files, changes, creations, deletions. User space programs like a desktop search engine can connect to Inotify, and ask it to send reports about the event X on file Y. If the event takes place, Inotify reports to the user space program, which for example re-indexes the file again.

For shell environments tools are already available to do talk to Inotify, settings watchers for certain files and directories, and receiving the reports: inotify-tools.

The inotify-tools contain two main programs: inotifywait and inotifywatch. The second one, inotifywatch, simply gathers statistics about file events. Much more interesting though is the first tool, inotfiywait. It asks Inotify to watch files and directories (even recursively) and it can call arbitrary actions on various Inotify events. The tool also offers the usual command line flags to make live easier: formatting strings for the output format, possibility to run as a daemon, read in file list from a file, write events to a file, exclude patterns, and so on.

For example: imagine you need to review a huge LaTeX script, thus occasionally have to change some smaller things but do not want to fire up “make” each time you changed something. If you are lazy, you can simply use inotifywait to monitor the directory containing your LaTeX files and calling make each time something changes:

$ while true; do inotifywait -r -e modify --exclude=".swp" . && make; done Setting up watches. Beware: since -r was given, this may take a while! Watches established. ./mainmatter/ MODIFY File15.tex latexmk -pdf -r ./pdflatexmkrc Latexmk: This is Latexmk, John Collins, 11 Nov. 2012, version: 4.35. **** Report bugs etc to John Collins <collins at phys.psu.edu>. **** Latexmk: applying rule 'pdflatex'... Rule 'pdflatex': File changes, etc: Changed files, or newly in use since previous run(s): 'mainmatter/File15.tex' ------------ Run number 1 of rule 'pdflatex' ------------

In the above example inotifywait is called with the flag -e modify which restricts it to only monitor modifying events. Also temporary vim files are excluded and thus ignored. The watches are set up, the program is waiting – until Inotify reports the event “MODIFY” on “File15.tex” in the sub-directory “mainmatter”. Afterwards, “make” was called, which again launched “latexmk”. Also the while loop ensures that after a change inotifywait is called again and thus monitors the files and directories.

Summarizing I can say inotifywait is an easy and quite usable approach to access the Inotify subsystem in simple and daily shell scripts and tasks. It makes life easier without making things complicated.

If you know people speaking German who are interested in this topic, I also wrote a German version of this howto for my employer’s blog.