Inotify-tools on CentOS: 5 Easy Fixes for Common Mistakes
The old version of this guide showed how to install inotify-tools on CentOS and gave a small monitoring script, but it was never run, and the script does not work for file names with spaces. It also gave no sign of the limits that a real watcher meets. This rewrite runs everything for inotify-tools on CentOS-family systems on Rocky Linux 9 in a container, because CentOS 7 reached end of life on 30 June 2024 and its repositories are no longer a good base for new work. Rocky is a CentOS-compatible rebuild of RHEL, so the commands for inotify-tools on CentOS-family systems are the same.
Contents
Install inotify-tools on CentOS and Rocky Linux
The package is not in the base repositories. On a fresh Rocky Linux 9 image, dnf did not find it until I enabled EPEL:
$ dnf -q install -y inotify-tools
Error: Unable to find a match: inotify-tools
$ dnf -q install -y epel-release
$ dnf -q install -y inotify-tools
$ rpm -q inotify-tools
inotify-tools-3.22.1.0-1.el9.x86_64
The old post said yum install inotify-tools would be enough. On this CentOS-family system that command alone fails, so the guide to inotify-tools on CentOS was incomplete from the first step. The package gives you two programs: inotifywait waits for events, and inotifywatch counts them. The rest of this post uses inotifywait. The project's own documentation is on the inotify-tools wiki.
What inotifywait prints and how to format it
This is the basic use of inotify-tools on CentOS and Rocky: I watched a directory with -m (stay running) and -r (recursive), then created, changed, moved and deleted files:
$ inotifywait -m -r -e create -e delete -e modify -e moved_from -e moved_to /data
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/data/ CREATE a.txt
/data/ MODIFY a.txt
/data/ MODIFY a.txt
/data/ MOVED_FROM a.txt
/data/ MOVED_TO b.txt
/data/ DELETE b.txt
/data/ CREATE,ISDIR d
/data/d/ CREATE my file.txt
Each line has three parts: the watched directory, the event names joined with commas, and the file name. Note that echo hi > a.txt produced two MODIFY events (the truncate and the write), and a directory carries the extra flag ISDIR. The last file name contains a space, and this is where parsing by whitespace starts to break. A custom format avoids guessing. With --format "%T|%w|%e|%f" and --timefmt "%F %T" the output becomes:
2026-11-05 07:15:39|/data/|CREATE|a.txt
2026-11-05 07:15:39|/data/|CREATE,ISDIR|d
2026-11-05 07:15:39|/data/d/|CREATE|my file.txt
The two "Setting up watches" lines go to standard error, so they do not mix with the events when you pipe standard output.
The old inotify-tools on CentOS script breaks on file names with spaces
The monitoring script for inotify-tools on CentOS from the old post used read FOLDER ACTION1 ACTION2 ACTION3 ACTION4 to split each event line, then wrote four lines per event. That design assumes one word per field. I ran it and created a file called my file.txt:
Path /data/ Create CREATE
Path /data/ Delete my
Path /data/ Modify file.txt
Path /data/ Move
One event became four lines of nonsense: the name was split at the space, and the words landed in the "Delete" and "Modify" fields even though nothing was deleted or modified. The fix is to read the whole line and split once, with a format that puts the event first. Here is a loop that survives spaces:
#!/bin/bash
inotifywait -m -r --format "%e %w%f" -e create -e delete -e modify -e moved_from -e moved_to /data |
while IFS= read -r line; do
event=${line%% *}; path=${line#* }
echo "$(date +%T) event=$event path=[$path]" >> /tmp/events.log
done
Because the event name never contains a space, everything after the first space is the path. The same operations, now including a move into a subdirectory, were logged correctly:
07:15:43 event=CREATE path=[/data/my file.txt]
07:15:43 event=CREATE,ISDIR path=[/data/d]
07:15:43 event=MOVED_FROM path=[/data/my file.txt]
07:15:43 event=MOVED_TO path=[/data/d/my file.txt]
07:15:43 event=DELETE path=[/data/d/my file.txt]
The old script used -e move, which also works, but separate moved_from and moved_to events let you see where a file went.

Do not use while inotifywait for every event
The old post's backup and log examples for inotify-tools on CentOS use while inotifywait -e modify ...; do ...; done. Each pass starts a new inotifywait, and events that happen while your loop body is busy are lost. I created ten files in a quick burst while the loop body took half a second:
files created: 10; events handled: 1
With -m, one long-running inotifywait feeds a loop and the kernel queues events for it. The same burst gave:
files created: 10; events handled: 10
The queue is not unlimited. The kernel manual page for inotify documents max_queued_events, and events over the limit are dropped with an overflow event. On this system it was 16384. If your handler is slow, hand the work to a queue or a background job instead of doing it in the read loop.
Fix "upper limit on inotify watches reached"
Recursive watching with inotify-tools on CentOS uses one watch per directory. The limit is a per-user setting, documented in the inotify(7) manual page. In my container it was 136140, and I created 140000 directories on a tmpfs to cross it:
$ cat /proc/sys/fs/inotify/max_user_watches
136140
$ inotifywait -m -r -e create /big
Setting up watches. Beware: since -r was given, this may take a while!
Failed to watch /big; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.
The error is clear, and the line "Watches established" never appeared, so the monitor did not start. Check for this message in the logs of any service that runs inotify-tools on CentOS servers with large trees. The other limit, max_user_instances, was 128 here. To raise the watch limit on a real server, put fs.inotify.max_user_watches=... in a file under /etc/sysctl.d/ and run sysctl --system; I could not write the value inside a rootless container, so I did not test the change. Each watch uses kernel memory, so raise it only as far as you need and watch fewer directories where possible.
Checklist for inotify-tools on CentOS
- Before you run inotify-tools on CentOS, enable EPEL first; the package is not in the base repositories.
- Use
--formatand read whole lines; never split output on whitespace. - Use
-mwith one reader instead ofwhile inotifywait. - Watch the smallest tree you can and check
max_user_watchesfor large ones. - Test your script with a file name that contains a space.
For more on writing shell scripts that behave the same everywhere, see my post on portable shell scripts.
Comments
Post a Comment