inotifywait

monitoringLinux
The inotifywait command is one of the most frequently used commands in Linux/Unix-like operating systems. inotifywait Wait for changes to files using inotify

Quick Reference

Command Name:

inotifywait

Category:

monitoring

Platform:

Linux

Basic Usage:

inotifywait [options] [arguments]

Common Use Cases

    Syntax

    inotifywait [options] [file/directory...]

    Options

    Option Description
    -m, --monitor Keep monitoring indefinitely, rather than exiting after first event
    -r, --recursive Watch directories recursively
    -e, --event EVENT Watch for specific event(s) only (access, modify, attrib, close_write, close_nowrite, close, open, moved_to, moved_from, move, move_self, create, delete, delete_self, unmount)
    --exclude PATTERN Exclude files matching this regular expression
    --excludei PATTERN Like --exclude but case insensitive
    --include PATTERN Include only files matching this regular expression
    --includei PATTERN Like --include but case insensitive
    -q, --quiet Print less (only print events)
    -t, --timeout SECONDS Exit if no event occurs in SECONDS seconds
    --format FORMAT Print using a specified format string (%w=path, %f=file, %e=event, %T=date/time)
    --timefmt FORMAT Date/time format for use with %T in --format string
    -c, --csv Output in CSV format
    -s, --syslog Send errors to syslog rather than stderr
    -h, --help Show help message
    -v, --verbose Be verbose
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the inotifywait command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    inotifywait /path/to/file
    Wait for a single change to occur to the specified file.
    inotifywait -m /path/to/directory
    Monitor a directory for changes continuously.
    # Advanced Examples Advanced
    inotifywait -m -r -e create,modify,delete /path/to/watch Monitor a directory recursively for create, modify, and delete events. inotifywait -m --format '%w%f %e' -e modify /var/log Monitor the /var/log directory and output the filename and event type when files are modified. inotifywait -m -e access -e modify /path/to/file | while read line; do echo "File $line accessed or modified"; done Monitor a file and trigger a custom action when it's accessed or modified. inotifywait -m -r --exclude '.*\.tmp$' /path/to/watch Monitor a directory recursively but exclude temporary files with .tmp extension. inotifywait -m -r --timefmt '%H:%M:%S' --format '%T %w%f %e' /path/to/watch Monitor a directory with custom time format and output formatting. inotifywait -m -e create --exclude '^\.' /path/to/watch Monitor a directory for file creation events but ignore hidden files.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The inotifywait command is a powerful utility that uses Linux's inotify subsystem to wait for changes to files and directories. It's part of the inotify-tools package and is particularly useful for monitoring file system events in real-time, allowing for immediate responses to file operations. Key features of inotifywait: 1. Event Monitoring: inotifywait can watch for specific file system events such as file creation, modification, deletion, access, attribute changes, and more. This granular control allows you to respond only to events that matter for your use case. 2. Continuous Monitoring: With the -m (monitor) option, inotifywait can run indefinitely, continuously reporting file system changes rather than exiting after the first event. 3. Recursive Watching: The -r option enables recursive monitoring of directories, making it possible to watch entire directory trees with a single command. 4. Flexible Output Formatting: Using the --format option, you can customize how event information is displayed, making it easier to parse or integrate with other tools. 5. Filtering Capabilities: You can include or exclude files from monitoring based on regular expressions, allowing you to focus on specific files or ignore certain patterns. 6. Timeout Functionality: The --timeout option allows inotifywait to exit if no events occur within a specified period, which can be useful in scripts that shouldn't wait indefinitely. 7. Script Integration: inotifywait is designed to be easily integrated into shell scripts, making it a powerful tool for automating responses to file system events. inotifywait is commonly used for: - Triggering builds when source files change during development - Automatically processing files as they are added to a directory - Implementing simple file synchronization solutions - Monitoring log files for changes - Creating simple backup systems that respond to file modifications - Setting up development environments with automatic reloading It's important to note that inotifywait depends on the Linux inotify API, so it's not available on non-Linux systems. Additionally, there are system limits on the number of inotify watches that can be established, which may need to be increased when monitoring large directory structures.

    Related Commands

    These commands are frequently used alongside inotifywait or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the inotifywait command works in different scenarios.

    $ inotifywait
    View All Commands