Search code examples
linuxbashechomonitoringinotifywait

Using inotify in a script to monitor a directory


I have written a bash script to monitor a particular directory "/root/secondfolder/" the script is as follows:

#!/bin/sh

while inotifywait -mr -e close_write "/root/secondfolder/"
do
    echo "close_write"
done

When I create a file called "fourth.txt" in "/root/secondfolder/" and write stuff to it, save and close it, it outputs the following but it does not echo "close_write":

/root/secondfolder/ CLOSE_WRITE,CLOSE fourth.txt

can someone point me in the right direction?


Solution

  • It turns out all I had to do was pipe the command into a while loop:

    !/bin/sh
    
    inotifywait -mqr -e close_write "/root/secondfolder/" | while read line
    do
    echo "close_write"
    done