Bash Script to Monitor Directory Changes Using inotifywait
Use
inotifywait -m /path/to/dir in a Bash script to monitor directory changes continuously and print events as they happen.Examples
InputMonitor /tmp directory
OutputSetting up monitoring on /tmp
Event detected: MODIFY file.txt
InputMonitor /home/user/docs directory
OutputSetting up monitoring on /home/user/docs
Event detected: CREATE newfile.txt
InputMonitor empty directory /emptydir
OutputSetting up monitoring on /emptydir
(No events until a change occurs)
How to Think About It
To monitor directory changes, the script uses a tool that listens for file system events like creation, deletion, or modification. It waits and prints these events as they happen, so you can see changes live.
Algorithm
1
Get the directory path to monitor as input2
Use a system tool to watch for file system events in that directory3
Print each event as it occurs4
Keep running to continuously monitor changesCode
bash
#!/bin/bash DIR_TO_WATCH="$1" if [ -z "$DIR_TO_WATCH" ]; then echo "Usage: $0 /path/to/directory" exit 1 fi if ! command -v inotifywait &> /dev/null; then echo "Please install inotify-tools package to use this script." exit 1 fi echo "Monitoring directory: $DIR_TO_WATCH" inotifywait -m -e create -e modify -e delete "$DIR_TO_WATCH" | while read path action file; do echo "Event detected: $action $file" done
Output
Monitoring directory: /tmp
Event detected: CREATE example.txt
Event detected: MODIFY example.txt
Event detected: DELETE example.txt
Dry Run
Let's trace monitoring /tmp directory with a file creation event
1
Start script with directory /tmp
DIR_TO_WATCH=/tmp
2
Check if inotifywait is installed
inotifywait found
3
Start monitoring /tmp
Waiting for events...
4
File example.txt created
Output: Event detected: CREATE example.txt
| Step | Action | Output |
|---|---|---|
| 1 | Set DIR_TO_WATCH=/tmp | |
| 2 | Check inotifywait | Found |
| 3 | Start monitoring | Waiting for events |
| 4 | File created | Event detected: CREATE example.txt |
Why This Works
Step 1: Using inotifywait
The script uses inotifywait which listens to file system events in real time.
Step 2: Monitoring specific events
It watches for create, modify, and delete events to catch common changes.
Step 3: Continuous monitoring
The -m flag keeps the command running to report events as they happen.
Alternative Approaches
Polling with ls and sleep
bash
#!/bin/bash DIR="$1" PREV=$(ls "$DIR") while true; do sleep 2 CURR=$(ls "$DIR") if [ "$PREV" != "$CURR" ]; then echo "Change detected in $DIR" PREV="$CURR" fi done
Simple but inefficient; checks directory contents every 2 seconds instead of event-driven.
Using fswatch tool
bash
#!/bin/bash fswatch -0 "$1" | while read -d "" event; do echo "Change detected: $event" done
Requires fswatch installed; works on multiple platforms but less common than inotifywait on Linux.
Complexity: O(1) per event time, O(1) space
Time Complexity
The script waits for events and processes each as it occurs, so time per event is constant.
Space Complexity
Uses minimal memory to store event info and directory path; no large data structures.
Which Approach is Fastest?
Using inotifywait is fastest and most efficient compared to polling or other tools.
| Approach | Time | Space | Best For |
|---|---|---|---|
| inotifywait | O(1) per event | O(1) | Real-time, efficient monitoring |
| Polling with ls | O(n) every interval | O(n) | Simple but inefficient, cross-platform |
| fswatch | O(1) per event | O(1) | Cross-platform, requires extra tool |
Install inotify-tools package to use
inotifywait for efficient directory monitoring.Forgetting to install inotify-tools or not passing the directory path as an argument causes the script to fail.