LinuxCommandLibrary

inotify-wait

Wait for file system events

TLDR

Run a command when a file changes

$ while inotifywait [path/to/file]; do [command]; done
copy

Be quiet about watching for changes

$ while inotifywait --quiet [path/to/file]; do [command]; done
copy

Watch a directory recursively for changes

$ while inotifywait --recursive [path/to/directory]; do [command]; done
copy

Exclude files matching a regular expression

$ while inotifywait --recursive [path/to/directory] --exlude '[regex]'; do [command]; done
copy

Wait at most 30 seconds

$ while inotifywait --timeout [30] [path/to/file]; do [command]; done
copy

Only watch for file modification events

$ while inotifywait --event [modify] [path/to/file]; do [command]; done
copy

SYNOPSIS

inotifywait [options] path... [--] [event1 [event2 ...]]

PARAMETERS

-h, --help
    Display usage help and exit

-V, --version
    Print version information

@file
    Exclude specified file from watching

--fromfile file
    Read watch paths from file or stdin

-c, --csv
    Output events in CSV format

-d, --daemon
    Daemonize, write PID to inotifywait.pid

--device
    Show device name, not inode

-e event, --event event
    Watch specific events only (default: all)

--exclude "pattern"
    Skip files matching pattern

--excludei "pattern"
    Case-insensitive pattern exclude

--format "string"
    Custom printf-style output

-m, --monitor
    Continuous monitoring, no single-event exit

-q, --quiet
    Suppress non-event messages

-r, --recursive
    Recursively watch directories

--timefmt "format"
    strftime format for timestamps

-t seconds, --timeout seconds
    Exit after timeout without events

-T
    Suppress timefmt on timeout

DESCRIPTION

inotifywait is a command-line utility from the inotify-tools package that efficiently monitors changes to files and directories on Linux systems via the kernel's inotify API, introduced in kernel 2.6.13. It blocks until a specified event occurs—such as file access, modification, creation, deletion, or movement—and then prints details to stdout, including the event name, affected file, and timestamp if formatted.

By default, it exits after the first event, but -m enables continuous monitoring, making it perfect for long-running scripts. Recursive directory watching (-r) handles subdirectories, while options like --format and --timefmt allow customizable output, including printf-style strings with placeholders like %w (watched dir), %f (filename), %e (events).

Common use cases include triggering actions in shell scripts, like recompiling code on saves, tailing logs reactively, or syncing files. Events are categorized into types like ACCESS, MODIFY, CREATE, DELETE, OPEN, CLOSE, MOVED_TO, and more, filterable with -e. It supports exclusions via patterns or files for efficiency.

Performance is lightweight, but watch limits apply system-wide.

CAVEATS

Linux-only (kernel 2.6.13+); no NFS/remote FS support; limited by /proc/sys/fs/inotify/max_user_watches; queue overflow possible (Q_OVERFLOW event); high watch counts increase kernel memory use.

KEY EVENTS

ACCESS: File read
MODIFY: Content changed
ATTRIB: Metadata updated
CLOSE_WRITE: Close after write
CREATE: New file/dir
DELETE: File removed
MOVED_FROM/MOVED_TO: Rename/move
OPEN: File opened

EXAMPLE

inotifywait -m -r -e modify,create /dir
Watches /dir recursively for mods/creates forever.

HISTORY

Developed as part of inotify-tools by Rohan McGovern et al., released ~2005 alongside Linux kernel 2.6.13's inotify support. Evolved for stable event monitoring in scripts; current versions (3.x+) add features like CSV and daemon mode.

SEE ALSO

inotifywatch(1), inotify(7), stat(1)

Copied to clipboard