LinuxCommandLibrary

inotifywait

TLDR

Watch a specific file for events, exiting after the first one

$ inotifywait [path/to/file]
copy
Continuously watch a file for events
$ inotifywait -m [path/to/file]
copy
Watch a directory recursively for events
$ inotifywait -m -r [path/to/directory]
copy
Watch a directory excluding files matching regex
$ inotifywait -m -r --exclude "[regex]" [path/to/directory]
copy
Watch with a timeout of 30 seconds
$ inotifywait -m -t 30 [path/to/file]
copy
Watch only for specific events (modify)
$ inotifywait -e modify [path/to/file]
copy
Watch quietly (no status messages)
$ inotifywait -q [path/to/file]
copy
Run a command when a file is accessed
$ inotifywait -e access [path/to/file] && [command]
copy

SYNOPSIS

inotifywait [options] file...

DESCRIPTION

inotifywait uses Linux's inotify API to efficiently watch files and directories for changes. It blocks until a filesystem event occurs, making it ideal for triggering actions on file changes.
Available events include:
- access - File read
- modify - File written
- create - File/directory created
- delete - File/directory deleted
- move - File moved
- attrib - Metadata changed
- close_write - File closed after writing
This is commonly used in scripts to rebuild projects, sync files, or trigger deployments when source files change.

PARAMETERS

-m, --monitor

Keep running, don't exit after first event
-r, --recursive
Watch directories recursively
-e, --event EVENT
Watch for specific events (access, modify, create, delete, etc.)
-t, --timeout SECONDS
Exit after timeout with no events
-q, --quiet
Suppress informational messages
--exclude PATTERN
Exclude files matching regex pattern
--format FMT
Custom output format
-c, --csv
Output in CSV format

CAVEATS

Linux-only (uses inotify kernel subsystem). There are limits on the number of watches (configurable via /proc/sys/fs/inotify/maxuserwatches). Recursive watching creates a watch per directory. Not suitable for very large directory trees.

HISTORY

inotifywait is part of inotify-tools, created to provide user-space access to Linux's inotify API, which was introduced in kernel 2.6.13 (2005). It replaced the older dnotify mechanism.

SEE ALSO

inotifywatch(1), entr(1), fswatch(1), fatrace(1)

Copied to clipboard