Directory and file monitoring in CentOS 7 with inotify

Daniel Aguilar
1 min readJul 17, 2021

--

When we talk about computers and operative systems is inevitable to talk about files and directories. Infinite files and directories are changing , creating and deleting simultaneously and everytime.

Some files are critical, and if you are a sysadmin you probably had issues in the past by a unwanted misconfigured or deleted file. So, always is a good idea to have an eye watching all events in those files or directories.

In this tutorial, I will show you how to monitor files and directories with inotify in a CentOS 7 system, but could be replicated in any Linux distro.

The goal for this tutorial is to detect every event in a directory and send a notification to an email.

So first things first. We need to enable EPEL repository and install inotify-tools package. Remember to do all this steps with a user with sudo privileges.

sudo yum install epel-release
sudo yum install inotify-tools

The next step is to create the script that is responsible of watching the directory o file. I usually create my custom scripts in /usr/local/bin directory, but you can create it wherever is better for you.

sudo touch /usr/local/bin/inotify_email_watcher.sh

And paste the following code:

And finally give the file execution permissions.

sudo chmod +x /usr/local/bin/inotify_email_watcher.sh

To test it, run the following command:

inotify_email_watcher.sh <path_to_file_or_dir> your.email@example.com

--

--

Daniel Aguilar
Daniel Aguilar

Written by Daniel Aguilar

System Administrator - Electronic Engineer

Responses (1)