inotifywait is a command-line tool that uses the inotify Linux kernel subsystem to watch for changes to one or more files or directories. It can be used to monitor a file or directory for modifications, such as changes to the file’s contents or attributes, or for events such as a file being created or deleted.
When a change is detected, inotifywait will output the event and the file or directory that it occurred on to the console. The tool can also be used to execute a specific command when a change is detected.
inotifywait Command Examples
1. Watch a specific file for events, exiting after the first one:
# inotifywait /path/to/file
2. Continuously watch a specific file for events without exiting:
# inotifywait --monitor /path/to/file
3. Watch a directory recursively for events:
# inotifywait --monitor --recursive path/to/directory
4. Watch a directory for changes, excluding files, whose names match a regular expression:
# inotifywait --monitor --recursive --exclude "regular_expression" /path/to/directory
5. Watch a file for changes, exiting when no event occurs for 30 seconds:
# inotifywait --monitor --timeout 30 /path/to/file
6. Only watch a file for file modification events:
# inotifywait --event modify /path/to/file
7. Watch a file printing only events, and no status messages:
# inotifywait --quiet path/to/file
8. Run a command when a file is accessed:
# inotifywait --event access path/to/file && command