Automatically rebuilding on file-write with inotify

The problem

With projects that have an incremental or otherwise very quick build, it's disrupting to have to switch back to a terminal to run build commands when editing.

A reasonably effective solution

There's a linux library/utility called inotify that's meant to solve this exact problem. I usually see it employed as a library utilized in other scripts or build tools, so the shell-based solution isn't the most googleable thing ever.

You'll need the inotify tools to get access to the command we need. Under Debian these are packaged as inotify-tools.

inotifywait is a command that watches inodes for a given event, then simply exits. This is useful for simply moving to the next iteration of a loop.

  while true; do
    inotifywait -e modify file.txt && echo 'file.txt modified';

I usually use it to quietly rebuild when any source file in the current project is modified.

  while true; do
    inotifywait -qre modify ./src