The working form first, since this page is otherwise all warnings. Use an explicit --format with a separator that cannot appear in the event field, with the path last, and parse it with IFS= and read -r so filenames survive intact:
inotifywait -m -r -e create,modify,delete,move --format '%e|%w%f' /path |
while IFS='|' read -r event target; do
printf 'event=%s target=%s\n' "$event" "$target"
done
That line was not executed here — there is no inotify on macOS, and everything below that is Linux-specific is quoted from inotify(7) and the kernel source rather than run. What follows is why the naive version of it goes quiet.
Before you build anything on inotifywait -m -r, know its two failure modes: it can run out of watches, and it can drop events — and in both cases what you get is a monitor that keeps running and reports nothing. A recursive watch costs one watch per directory, and if the tree outgrows fs.inotify.max_user_watches, the directories past the limit are simply never watched. For anything security- or compliance-related that is disqualifying.
This page is cited, not executed. inotify is a Linux kernel interface and there is no Linux machine here, so every number and quote below comes from inotify(7), inotify_add_watch(2) or the kernel source, named where it is used. The one block that was run is marked as macOS, and is an analogue rather than a substitute.
Table of Contents
The real defaults, from the kernel source
Two of the three limits are constants; the interesting one is not. From fs/notify/inotify/inotify_user.c in the mainline kernel:
/*
* Allow up to 1% of addressable memory to be allocated for inotify
* watches (per user) limited to the range [8192, 1048576].
*/
watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
INOTIFY_WATCH_COST;
watches_max = clamp(watches_max, 8192UL, 1048576UL);
...
inotify_max_queued_events = 16384;
init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
So the widely repeated “the default is 8192” is the floor, not the default: with enough RAM the kernel already gives you up to 1048576. Read the value before changing it — cat /proc/sys/fs/inotify/max_user_watches — because setting 1048576 may be a no-op. The limit is per real user ID: your editor’s file watcher, your dev server and your monitor all draw from one pool.
Running out is an error; dropping events is not
Separate these two, because only one is loud.
Watch exhaustion is reported. inotify_add_watch(2) fails with ENOSPC — “The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.” A well-written tool prints that. A shell loop piping inotifywait into while read without checking stderr will not.
Queue overflow is not. inotify(7): “Note that the event queue can overflow. In this case, events are lost.” The kernel sets one IN_Q_OVERFLOW event (“wd is -1 for this event”) and moves on. A consumer that forks a command per line falls behind a burst, the queue passes max_queued_events, and the events you needed disappear with no error and no gap in the log to point at.
A third one is quieter still. inotify(7), on recursive watching: “If monitoring an entire directory subtree, and a new subdirectory is created in that tree or an existing directory is renamed into that tree, be aware that by the time you create a watch for the new subdirectory, new files (and subdirectories) may already exist inside the subdirectory.” Everything created in that window is invisible. Rescan each directory yourself after adding its watch.
Why inotifywait cannot audit access to a secure directory
Take a script meant to log unauthorized access to a secure directory:
inotifywait -m -r -e access "$SECURE_DIR" |
while read path action file; do
echo "Unauthorized access to $file at $(date)" >> "$LOG_FILE"
done
It logs “Unauthorized” for every read of every file, and it cannot know either half of that word. inotify(7) is explicit: “The inotify API provides no information about the user or process that triggered the inotify event.” No UID, no PID — nothing to distinguish a backup job from an intruder, and nothing to put in an audit record. If you need attribution, that is auditd (or fanotify, which can report the PID); no amount of scripting adds the missing field.
Two more problems in those four lines. -e access across a recursive tree fires on every read, so a busy directory outruns a loop that forks date per event — the overflow case above, by design rather than accident. And inotify(7) notes it “does not catch remote events that occur on network filesystems”, so an NFS-exported “secure directory” is not covered.
Then the parsing. read path action file splits on whitespace, so a space in the watched directory shifts every column and the filename lands in the wrong variable; without -r, backslashes in names are eaten as well. The recipe at the top of this page avoids both. Following a log file with tail demonstrates the same shell failure in detail, with a corrected script — rather than repeat it here, use the --format form above.
Raising max_user_watches permanently
The form echo 524288 | sudo tee -a /proc/sys/fs/inotify/max_user_watches goes wrong twice. -a appends, which is not how you set a procfs tunable — drop it. And nothing written to /proc survives a reboot, so sudo tee -a does not make it permanent. The persistent form is a file under /etc/sysctl.d/:
# /etc/sysctl.d/60-inotify.conf
fs.inotify.max_user_watches = 524288
then sudo sysctl --system. Raising it is not free: each watch pins kernel memory for its lifetime, which is why the kernel caps the default at 1% of RAM.
Not Linux, but the same shape of failure — executed here
macOS has no inotify. Its nearest equivalent, kqueue, needs an open file descriptor per watched directory, so it hits RLIMIT_NOFILE where Linux hits max_user_watches. Registering every directory under a package tree on macOS 26.6, descriptor limit lowered to 256:
RLIMIT_NOFILE soft=256 hard=256
directories under /opt/homebrew/Cellar: 6373
watches established: 252
stopped at: [Errno 24] Too many open files: '/opt/homebrew/Cellar/xorgproto/2025.1/share/doc/bigreqsproto'
252 of 6373 directories — 96% of the tree unwatched. The watcher is still running, still healthy, still reporting events; it just cannot see almost anything. That is the shape of the Linux failure too. Make your monitor log its established watch count and compare it against the directory count before you trust a quiet log. Scaled down to 400 directories and 128 descriptors, the macOS run above is repeated by inotify-watch-file-system-events.sh, which also replays the read whitespace and backslash cases and, on Linux, reads the kernel’s inotify limits without ever writing to /proc or /etc.
For following a single file rather than a tree, tail -F is simpler and has no watch budget at all.