emcrisostomo / fswatch

A cross-platform file change monitor with multiple backends: Apple OS X File System Events, *BSD kqueue, Solaris/Illumos File Events Notification, Linux inotify, Microsoft Windows and a stat()-based backend.
https://emcrisostomo.github.io/fswatch/
GNU General Public License v3.0
5.01k stars 326 forks source link

Could I differentiate add, delete, and modified method #162

Closed ctrngk closed 7 years ago

ctrngk commented 7 years ago

Hi,

I want to append .ok after every new file found in test directory.

➜ test fswatch -0 . | xargs -0 -I {} mv {} {}.ok mv: /Users/mac/test/cc: No such file or directory mv: /Users/mac/test/cc.ok: No such file or directory mv: /Users/mac/test/cc.ok.ok: No such file or directory mv: /Users/mac/test/cc.ok.ok.ok: No such file or directory mv: /Users/mac/test/cc.ok.ok.ok.ok: No such file or directory

After touch cc, It makes sense because fswatch detects new cc, and renames it as cc.ok, detects this new file and continues to rename as cc.ok.ok, to endless journey.

Possible solution 2: ➜ testfswatch -0 -e '\.ok$' -1 . | xargs -0 -I {} mv {} {}.ok This works, but the limitation -1 option might not be useful for background long-run task.

Possible solution 3: ➜ test fswatch -0 -e '\.ok$' . | xargs -0 -I {} mv {} {}.ok mv: /Users/mac/test/aa: No such file or directory The wired issue: After touch aa, It executed two times, instead of one, for each new file. I did not know why.

Any possible solutions to get around this? Please advise. Thank you.

Regards,

ctrngk commented 7 years ago

I have figured it out. The reason why solution 3 execute two times, is that touch aa

  1. new file aa added to directory, detected, and rename to aa.ok.
  2. aa deleted, also detected, and rename to aa.ok, which raise error.

Although solution 3 still works, the log file is not pretty. So the question change to: Could I differentiate add, delete, and modified method?

ctrngk commented 7 years ago

fswatch -x path will solve this problem. Case closed.