Using inotify-tools to watch a directory and take action

The inotify-tools can be used to watch a directory or file for activity and take an action when a file is changed, added, edited or simply read. You can find a workaround (or call it solution) for the absence of the inotify-tools in the repositories here.

Sometimes, for various reasons, a directory needs to be watched for new files, file changes, etc. When a change occurs, a certain action should be taken, like copying the files elsewhere or send an email with to notify someone of the changes. There are a lot of such imaginable scenarios to think of.


The inotify mechanism which exists in the Linux kernel since release 2.6.13 is ideal for purposes as described above. Inotify notifies the kernel on file changes so it’s not needed to explicitly scan the file system for changes on a regular basis. To use inotify, there are various packages available for different programming languages (like Perl or Python).


The package inotify-tools contains some tools, written in C, to easily access the inotify-mechanism via the command line or bash.

Now that we have a working installation of the inotify-tools, we can start using them.

The easiest way to use the tools is to create a small bash-script that will monitor a directory and takes action when something is added or changed in that directory. As an example, I created this script,, that logs all actions that were taken on a certain directory.

Contents of the script:

Don’t forget to make the file executable, then execute the script in the background (by putting the & after the command) and redirect it’s output to /dev/null in order to keep our shell clean:

When do some basic actions on the folder which we are watching, we should see all actions taken in the logfile:

To stop the script, simply bring it back to the foreground and stop it with Ctrl + C:

Looking at the log, we can see that the above executed actions: copy, move, modify and open are logged perfectly to our watchlog.txt

The above script is just a small example what you can do with the inotify-tools. Besides logging you could take other actions like moving, changing or executing something that is related to the files that were touched in the watched directory.