Inotifywait in Linux: Automatically Rebuild Projects on File Changes

Quick Linux Tip #116:

Question: How do I auto-rebuild my project when source files change?

Try: while inotifywait -r -e modify,create,delete ./src/; do make build; done

Info: inotifywait exits after a single file event, allowing a simple while loop to trigger builds on modification without external watchers.

Examples:

  • $ while inotifywait -e modify ./style.css; do sass compile; done  # Recompile CSS on change
  • $ ls ./src/*.go | entr make build  # Alternative using the lightweight 'entr' tool
  • $ fswatch -o ./src | xargs -n1 -I{} make build  # Cross-platform alternative file watcher

Note: Install via sudo apt install inotify-tools. Avoid recursive (-r) monitoring on massive directory trees to prevent performance lag; target specific paths instead.




LinuxTeck.com
linuxteck:~$ while inotifywait -r -e modify,create,delete ./src/; do gcc -o app main.c utils.c; done
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
./src/ MODIFY main.c
gcc -o app main.c utils.c

Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
./src/ MODIFY utils.c
gcc -o app main.c utils.c

Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
./src/ CREATE new_module.c
gcc -o app main.c utils.c new_module.c
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #115: Linux ulimit Memory Limit: 3 Commands to Control RAM Usage NEXT ARTICLE Quick Linux Tip #117: Linux Process Environment Variables: 3 Commands to Inspect Running Daemons
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.