inotify is a Linux kernel subsystem and API that notifies user-space programs about filesystem events, such as files being created, modified, moved or deleted, so applications can react immediately instead of repeatedly scanning directories. It has been part of the mainline kernel since version 2.6.13 in 2005, where it replaced the older and clumsier dnotify mechanism.
In the Linux filesystem stack, inotify sits above the virtual filesystem layer, which means it works with ext4, XFS, Btrfs, tmpfs and most other local filesystems. Tools such as file synchronizers, build systems, desktop file managers, log shippers and systemd path units all depend on it.
How inotify delivers filesystem events
inotify works through a file descriptor-based interface built on three system calls. A program calls inotify_init1() to obtain an inotify instance, adds one watch per file or directory with inotify_add_watch(), and then reads a stream of event records from the descriptor with read(), typically inside a poll(), select() or epoll loop.
Each record is a struct inotify_event containing a watch descriptor, an event mask, a cookie that links the two halves of a rename, and, for events inside a watched directory, the name of the affected entry. The kernel queues events per instance and merges identical consecutive events to limit queue growth.
| Call | Purpose | Returns |
|---|---|---|
inotify_init1() | Creates a new inotify instance | File descriptor for reading events |
inotify_add_watch() | Watches a path for selected events | Watch descriptor, an integer |
inotify_rm_watch() | Removes a watch | Zero on success |
read() | Retrieves queued event records | One or more event structures |
close() | Destroys the instance and all watches | Zero on success |
Watches are tied to inodes, not path names. If a watched file is renamed, the watch follows the inode; if it is deleted and replaced, the old watch ends with IN_IGNORED and the program must add a new one.
Event types an inotify watch can report
An inotify watch reports only the events selected in its bit mask, so a program can subscribe narrowly and reduce noise. The most widely used flags are listed below; several can be combined, and IN_ALL_EVENTS selects them all.
| Flag | Triggered when | Typical use |
|---|---|---|
| IN_CREATE | An entry is created in a watched directory | Detecting new uploads |
| IN_CLOSE_WRITE | A file opened for writing is closed | Processing a file once fully written |
| IN_MODIFY | File content is written | Live preview or tailing |
| IN_MOVED_FROM / IN_MOVED_TO | An entry is renamed or moved | Tracking renames with the cookie |
| IN_DELETE | An entry is removed from a watched directory | Cleaning up indexes or caches |
| IN_ATTRIB | Metadata such as permissions or timestamps change | Reacting to chmod or touch |
| IN_Q_OVERFLOW | The event queue overflowed | Triggering a full rescan |
A common best practice is to act on `IN_CLOSE_WRITE` rather than `IN_MODIFY` when processing incoming files, because a single large write produces many modify events while the file is still incomplete.
inotify compared with other change-detection methods
inotify is the standard per-object notification API on Linux, but it is not the only option. Polling remains universal, dnotify is its obsolete predecessor, and fanotify offers mount-wide or filesystem-wide monitoring aimed at security and auditing software.
| Method | Scope | Strengths | Weaknesses |
|---|---|---|---|
| Polling | Any path | Works everywhere, including network shares | Slow reaction and wasted CPU and I/O |
| dnotify | Directories only | Historical compatibility | Uses signals and holds directories open |
| inotify | Individual files and directories | Precise, efficient, widely supported | No recursion; one watch per directory |
| fanotify | Mounts or whole filesystems | Scales to large trees; can allow or deny access | Needs elevated privileges for many features |
On other platforms the equivalent roles are filled by kqueue on BSD and macOS, FSEvents on macOS, and ReadDirectoryChangesW on Windows. Cross-platform libraries wrap these differences behind a single interface.
Practical use and kernel limits
Administrators usually meet inotify through the inotify-tools package, which provides inotifywait for blocking until an event occurs and inotifywatch for gathering event statistics. A command such as inotifywait -m -e close_write /srv/incoming prints each completed file, which makes simple shell automation easy.
Because inotify watches are not recursive, a program that monitors a tree must add a watch to every subdirectory and add new ones as directories appear. Large trees therefore hit the per-user kernel limits, which are tunable under /proc/sys/fs/inotify/.
| Setting | Controls | Effect of raising it |
|---|---|---|
| max_user_watches | Total watches per user | Allows monitoring larger directory trees |
| max_user_instances | inotify instances per user | Allows more concurrent watching programs |
| max_queued_events | Queue length per instance | Reduces the risk of overflow during bursts |
Defaults vary by kernel version and available memory; recent kernels size max_user_watches automatically, often in the tens of thousands to over a million. Each watch consumes a small amount of unswappable kernel memory, so the value should match real need, and it is set persistently with a file under /etc/sysctl.d/.
Limitations and common mistakes with inotify
The most frequent misunderstanding is expecting inotify to see every change on every filesystem. It reports only changes made through the local kernel, so edits made by another client on NFS, SMB or other network filesystems are usually invisible, and many FUSE and pseudo filesystems such as /proc and sysfs report little or nothing.
- Queue overflow loses events: when
IN_Q_OVERFLOWarrives, the program must rescan instead of trusting its state. - Race conditions on new directories: files created before a watch is added to a fresh subdirectory are missed unless the program scans it.
- No user or process information: events do not say who made the change; fanotify or the audit subsystem is needed for that.
- Exhausted watch limits produce the error message “no space left on device”, which misleadingly suggests a full disk.
- Renames need pairing: matching
IN_MOVED_FROMandIN_MOVED_TOby cookie is required to tell a rename from a delete plus create.
Robust applications therefore treat inotify as a fast hint, not a complete journal, combining event handling with periodic reconciliation scans.
In simple terms
Imagine waiting for a letter by walking to the mailbox every five minutes. That is how a program behaves without inotify: it keeps checking folders to see whether anything changed. inotify is like a doorbell that rings the moment a file arrives, changes or disappears.
Programs ask the Linux kernel to watch certain files or folders, and the kernel tells them as soon as something happens. This saves time and computing power, and it is why tools that sync files or rebuild software can react almost instantly.
A practical example
A printing company receives customer artwork through an SFTP server running Linux. Files land in /srv/upload, and each one must be converted to a print-ready PDF. Previously, a cron job scanned the folder every minute, so customers waited up to 60 seconds and partially uploaded files were sometimes processed by mistake.
The administrator replaces the cron job with a small service that runs inotifywait -m -e close_write on the upload directory and passes each finished file to the conversion script. Conversion now starts within a fraction of a second after an upload completes, and because the service reacts only to closed files, broken half-uploaded jobs disappear. A nightly scan remains as a safety net for any missed event.