TIL: Monitoring File Changes With inotifywait on Linux
Need to run a command when a file appears in a directory? inotifywait watches and waits.
inotifywait -e create -m /data/incoming/
Monitors the directory for new files. The -m flag keeps watching after the first event.
inotifywait -e close_write --format "%f" /data/incoming/ | while read FILE; do
python transcribe.py "/data/incoming/$FILE"
done
When a file finishes writing, inotifywait outputs its name. The while loop feeds each filename to your processing script.
inotifywait -r -e create /data/recordings/
The -r flag watches directories recursively. New files in subdirectories trigger the event too.
Is inotifywait available on macOS?
No, inotify is Linux specific. On macOS use fswatch or kqueue based watchers.
How do I install it?
sudo apt install inotify-tools
On Debian and Ubuntu based systems.