bashunixfswatch

fswatch write then delete


I am using fswatch to refresh refreshed.txt when watched.txt is modified. Currently, something is appended to the refreshed.txt.

 fswatch -o watched.txt | xargs -n1 sh -c "echo 'something' >> refreshed.txt"

However, I want to write something and then delete it.

I have tried:

fswatch -o watched.txt | xargs -n1 sh -c "echo 'something\b\b' >> refreshed.txt"

and

fswatch -o watched.txt | xargs -n1 sh -c "echo 'something' >> refreshed.txt && sed -ie '$d' refreshed.txt"

but neither seem to achieve the desired effect.


Solution

  • The sed is not run, because when you feed you " quoted script to sh, $d is replaced with an empty string:

    sed -ie '' refreshed.txt.
    

    You should escape $, to avoid that:

    sed -ie '\$d' refreshed.txt 
    

    Note that adding a line and then immediately removing it, would be pretty much an equivalent of simply updating it's modification timestamp, so you may want to make it sleep for a bit, for the change detection system to catch on.