linuxmacosinotifyinotifywaitfswatch

How to print file path that caused fswatch to do something


I use fswatch to take some actions when a file changes. I use the following command:

fswatch -o -r '.' | while read MODFILE
do
    echo 'Some file changed, so take some action'
done

this works fine, if I change a couple files I see the following in the terminal:

Some file changed, so take some action
Some file changed, so take some action
Some file changed, so take some action
etc.

But I also wonder which file actually caused the action. So I checked the fswatch man page, which shows the following:

fswatch writes a record for each event it receives containing:
     -       The timestamp when the event was received (optionally).
     -       The path affected by the current event.
     -       A space-separated list of event types (see EVENT TYPES ).

But as said I don't see anything listed in my terminal. Does anybody know how I can show "The path affected by the current event."?

All tips are welcome!


Solution

  • Sure, use this:

    fswatch --batch-marker=EOF -xn . | while read file event; do 
       echo $file $event
       if [ $file = "EOF" ]; then 
          echo TRIGGER
       fi
    done
    

    If you want to save the names of the affected files in a list, you can do that like this:

    #!/bin/bash
    fswatch --batch-marker=EOF -xn . | while read file event; do
        if [ $file = "EOF" ]; then
           echo TRIGGER
           echo Files: "${list[@]}"
           list=()
        else
           echo $file $event
           list+=($file)
        fi
    done