Inotify is short for index node notify. An index node (short inode) is a filesystem object (files and folder) in Unix-style operating systems such as Linux. The terminology is a bit crazy.

Anyway, inotify is an API that allows programs to monitor for changes to files or folders.

Install

To use it from the shell, we first need to install inotify-tools.

Debian

sudo apt install inotify-tools

Arch Linux

sudo pacman -S inotify-tools

Usage

A simple usage is to wait for changes to a file.

If you are using bash:

inotifywait ~/.bash_history

Or, for ZSH:

inotifywait ~/.zsh_history

Then try to open another terminal. As soon as a new shell is spawned, it will access its history file.

We can also wait for the file to be modified.

inotifywait --event modify ~/.bash_history

Try to type a command in another shell.

This is super neat in cases where you want to automate some action when a file changes. This is great for workflow automation.

Here is an example that restarts Docker services when docker-compose.yml is changed.

inotifywait -e close_write -m --format '%w %e' docker-compose.yml | while read file action; do
  echo "Event $action on $file"
  echo "Restarting docker services"
  docker compose restart
done

I’ve tested above with nano. Different editors actually handle saving files in various ways. vim for instance first saves to a temporary file, then replaces the original.

IDEs use inotify watches heavily, such as for re-indexing source code when dependencies change.