How to monitor a complete directory tree for changes in Linux?
I've done something similar using the inotifywait
tool:
#!/bin/bash
while true; do
inotifywait -e modify,create,delete -r /path/to/your/dir && \
<some command to execute when a file event is recorded>
done
This will setup recursive directory watches on the entire tree and allow you to execute a command when something changes. If you just want to view the changes, you can add the -m
flag to put it into monitor mode.
To my knowledge, there's no other way than recursively setting an inotify
watch on each directory.
That said, you won't run out of file descriptors because inotify
does not have to reserve an fd to watch a file or a directory (its predecessor, dnotify
, did suffer from this limitation). inotify
uses "watch descriptors" instead.
According to the documentation for inotifywatch, the default limit is 8192 watch descriptors, and you can increase it by writing the new value to /proc/sys/fs/inotify/max_user_watches
.
$ inotifywait -m -r /path/to/your/directory
This command is enough to watch the directory recursively for all events such as access, open, create, delete ...