inotifywait – Asynchronous file system monitoring mechanism

The inotifywait command is a Linux utility that is used for monitoring file system events. It is a part of the inotify-tools package, which includes other utilities such as inotifywatch and inotifywait-cmd. The inotifywait command is used to wait for changes to files or directories and then execute a command when a change occurs.

Overview

The inotifywait command has the following syntax:

inotifywait [options] [path...]

The path argument can be a file or directory to monitor. If no path is specified, the current directory is used.

The inotifywait command waits for changes to files or directories and then executes a command when a change occurs. The command to execute is specified using the -e option, which specifies the events to monitor. The events can be any combination of the following:

  • access: File or directory was accessed
  • modify: File or directory was modified
  • attrib: File or directory attributes were changed
  • close_write: File or directory was closed, after being opened for writing
  • close_nowrite: File or directory was closed, after being opened for reading
  • open: File or directory was opened
  • move: File or directory was moved or renamed
  • create: File or directory was created
  • delete: File or directory was deleted
  • delete_self: File or directory was deleted

For example, the following command monitors the /tmp directory for any file creation events:

inotifywait -e create /tmp

The inotifywait command will wait until a file creation event occurs in the /tmp directory and then exit.

Options

The following table lists the available options for the inotifywait command:

Option Description
-m, --monitor Monitor the specified path(s) indefinitely
-r, --recursive Monitor directories recursively
-q, --quiet Suppress output
-e <event>, --event <event> Specify events to monitor
-t <seconds>, --timeout <seconds> Specify a timeout in seconds
-h, --help Display help message
-v, --version Display version information

Troubleshooting Tips

  • If inotifywait is not detecting changes to a file or directory, ensure that the file or directory is not being modified by another process.
  • If you are monitoring a directory recursively, ensure that you have the necessary permissions to access all subdirectories.
  • If you are not seeing any output from inotifywait, ensure that the -q option has not been specified.

Notes

  • The inotifywait command can be used in shell scripts to trigger actions based on file system events.
  • The inotifywait command can be used in combination with other Linux utilities, such as rsync, to synchronize files between systems.
  • The inotifywait command can be resource-intensive when monitoring large directories or when monitoring directories recursively.