Watching Files for Changes on Linux with inotify

inotify is the Linux kernel API that lets a program watch a file or directory for changes without constantly polling — the mechanism behind tools like live-reload dev servers and file sync utilities.

sudo apt install inotify-tools

# Watch a directory for any changes, printing what happened
inotifywait -m -r /var/www/mysite -e modify,create,delete

# A practical use: trigger a script whenever a file changes
while inotifywait -e close_write /etc/myapp/config.yaml; do
    systemctl restart myapp
done

Why This Beats Polling

Polling a directory every few seconds to check for changes wastes CPU and introduces latency (up to the poll interval) before a change is even noticed. inotify instead has the kernel notify the watching process immediately when a relevant filesystem event occurs — instant reaction, essentially zero overhead when nothing is happening.

The Watch Limit

# Tools like VS Code, webpack watch mode, or large monorepos can hit
# the default max_user_watches limit
cat /proc/sys/fs/inotify/max_user_watches

echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p