Introduction
Inotify-tools is a set of utilities used to monitor filesystem events such as create, delete, add, and attribute changes. These tools provide a way to watch for changes in a directory or file and execute a command when an event occurs.
Installation
To install inotify-tools on a Debian-based system, use the following command:
sudo apt-get install inotify-tools
If the package is not found, you may need to install it from source or use a different package manager.
Using inotify-tools
The most common way to use inotify-tools is to run the "inotifywait" command followed by the directory or file you want to monitor and the events you want to watch for. For example:
sudo inotifywait /path/to/directory --event=create,delete,add,attr
This command will watch the specified directory for create, delete, add, and attribute changes, and execute the command immediately when an event occurs.
Using inotify-tools with a daemon
Inotify-tools can also be used with a daemon to continuously monitor a directory. To do this, run the following command:
sudo knownfs -d /path/to/working/directory
This command starts the "knownfs" daemon, which watches the specified working directory. You can then use inotifywait to watch for events in the working directory and execute commands when they occur.
Examples
Here are some examples of using inotify-tools with a daemon:
sudo knownfs -d /path/to/working/directory && sudo inotifywait /path/to/working/directory --event=create --format '%w %f' -e close_write --exec 'touch /path/to/touchfile'- This command watches the specified working directory for create events and executes the "touch" command on a file named "touchfile" when an event occurs.sudo knownfs -d /path/to/working/directory && sudo inotifywait /path/to/working/directory --event=delete --format '%w %f' -e close_write --exec 'rm /path/to/deletefile'- This command watches the specified working directory for delete events and executes the "rm" command on a file named "deletefile" when an event occurs.
References
- Inotify-tools documentation: https://man7.org/linux/man-pages/man1/inotify-tools.1.html
- knownfs man page: https://man7.org/linux/man-pages/man8/knownfs.8.html