2

I've just learnt how to constantly check if file is modified:

while inotifywait -q -e modify filename >/dev/null; do
    echo "filename is changed"
    # do whatever else you need to do
done

If I use a directoryname instead of a filename I can check all files of the directory:

while inotifywait -q -e modify directoryname >/dev/null; do
    echo "filename is changed"
    # do whatever else you need to do
done

But how can I echo the filename of the file that has been changed?

1
  • man inotifywait will tell you about the --format option. And while you're there, take a look at enhancing your loop with --monitor. Finally, don't discard the output of inotifywait; your answer is waiting for you there. Commented Feb 25, 2020 at 16:12

1 Answer 1

5

inotifywait emits a continuous set of events on the watched directory, so the recommended way would to move the watch out of the while loop and look for events within a new loop that looks on the output of the inotify. Note that this involves removing the --quiet flag, because the read command needs to see those events and read it over standard input.

inotifywait -m -e modify "directoryname" | 
while read -r dir action file; do
    echo "The file '$file' appeared in directory '$dir' via '$action'"
done
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .