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] file [file2] ...

PARAMETERS

-m, --monitor
    Stay listening for events indefinitely. Without this option, inotifywait exits after receiving one event.

-r, --recursive
    Watch all subdirectories of any directories passed as arguments. Watches are set up recursively to an unlimited depth.

-q, --quiet
    Print less to the terminal. Specifically, suppress the output of 'Establishing watches' and 'Setting up watches'.

-v, --verbose
    Give verbose output.

-d, --daemon
    Run in background as a daemon. Implies --monitor.

-o, --outfile
    Output events to rather than stdout.

-s, --syslog
    Output events to syslog rather than stdout.

-e, --event [,...]
    Listen for specific event(s) only. If not specified, all events are listened for. Common events include: access, modify, attrib, close_write, close_nowrite, close, open, moved_to, moved_from, move, create, delete, delete_self, unmount. See man page for full list.

-t, --timeout
    Exit after seconds of inactivity.

--format
    Print using a specified format. See man page for details on format sequences.

--timefmt
    strftime-compatible format for use with %T in --format option.

--exclude
    Exclude files matching from being watched. Uses regular expression matching.

--fromfile
    Add files to watch or exclude according to the options listed in . One file or exclude pattern per line.

-x, --excludei
    Case-insensitive version of --exclude.

--help
    Show help text and exit.

--version
    Show version information and exit.

DESCRIPTION

The inotify-wait command is a simple utility that uses the Linux inotify subsystem to monitor directories and files for specific filesystem events. It blocks execution until an event occurs on a watched file or directory and then exits, reporting the event that occurred. This makes it useful for triggering scripts or actions based on filesystem changes.

inotify-wait is frequently used in shell scripts for tasks such as automatically rebuilding code after a file is modified, triggering backups when files are changed, or simply logging filesystem activity.

Unlike polling-based solutions, inotify provides real-time notification of filesystem events with minimal overhead. It relies on the kernel to report changes. This makes it a very efficient and responsive way to react to filesystem modifications.

CAVEATS

The inotify subsystem has a limited number of watches that can be created per user. If this limit is reached, inotify-wait will fail. You can check the limit by reading /proc/sys/fs/inotify/max_user_watches and increase it by writing to /proc/sys/fs/inotify/max_user_watches as root.
Recursive watches can be resource-intensive for very deep directory structures.

EXIT STATUS

inotify-wait exits with status 0 if it successfully received at least one event. It exits with status 1 if an error occurred (such as being unable to watch a file, or running out of inotify watches). It exits with status 2 if it received no events due to the -t timeout expiring.

EVENTS

Access: File was accessed (read).
Modify: File was modified.
Attrib: Metadata changed (e.g., permissions, timestamps).
Close_write: File opened in write-only mode was closed.
Close_nowrite: File not opened in write-only mode was closed.
Close: Either close_write or close_nowrite occurred.
Open: File was opened.
Moved_to: File was moved into the watched directory.
Moved_from: File was moved out of the watched directory.
Move: Either moved_to or moved_from occurred.
Create: File/directory was created in the watched directory.
Delete: File/directory was deleted from the watched directory.
Delete_self: Watched file/directory was itself deleted.
Unmount: Filesystem was unmounted.

HISTORY

inotify-wait is part of the inotify-tools package, which was developed to provide command-line tools for utilizing the inotify subsystem. The inotify subsystem was introduced into the Linux kernel in version 2.6.13. The inotify-tools package gained popularity as a powerful and efficient way to monitor filesystem changes in scripts and applications.

SEE ALSO

inotifywatch(1)

Copied to clipboard