Report issue Add example

inotifywait

Wait for changes to files using inotify.

Description

Inotify is a powerful, fine-grained, asynchronous file system monitoring mechanism. It meets various file monitoring needs, such as tracking access, read/write operations, permission changes, deletions, creations, and moves.

inotify-tools is a C library and a set of command-line utilities that provide a simple interface to inotify. Installing inotify-tools provides two commands:

Check if your kernel supports inotify:

Installing inotify-tools

For CentOS:

tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
sudo make install

inotify Parameters

These parameters limit the kernel memory consumed by inotify:

To increase these values:

echo 104857600 > /proc/sys/fs/inotify/max_user_watches

inotifywait Usage

Example script (watchdir.sh):

#!/bin/bash
path=$1
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $path

Execution output:

./watchdir.sh /data/wsdata/tools/
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
...

inotifywait Options

Monitorable Events

Event Description
access File was read.
modify File content was modified.
attrib File metadata (permissions, timestamps, etc.) was modified.
move File was moved.
create A new file was created.
open File was opened.
close File was closed.
delete File was deleted.