emcrisostomo / fsw

A file change monitor with multiple backends: Apple OS X File System Events API, *BSD kqueue, Linux inotify and a stat-based backend.
http://emcrisostomo.github.io/fsw/
GNU General Public License v3.0
43 stars 7 forks source link

Ask for Help: filter file by extension name #10

Closed yi closed 10 years ago

yi commented 10 years ago

Hi I have tried -e and -E. But all failed, because I'm not familair with mac bash regexp.

Could you give me some hint. I wan to write a fsw command to do:

  1. only output changes of file with extension name: ".moon"
  2. call "moonc" every time file change captured.

thanks a lot!

emcrisostomo commented 10 years ago

Hi @yi, I strongly suggest you become familiar with regular expressions: there's a lot of literature on the Internet.

-e is meant to exclude files, so that I advise against following this path if all you want is filtering event file names. To filter events whose file extension is .ext you could use the following command:

fswatch -0 [options] ... [paths] ...  | while read -d "" i
do
  if echo $i | grep -q "^.*ext$" > /dev/null ; then
    // do something
  fi
done

If you want to keep things clean, you could use xargs and put all the logic you need to run (including, of course, the filtering logic) into a script.sh:

$ fswatch -0 [args] | xargs -0 -n 1 script.sh

Hope this helps, Enrico M. Crisostomo

yi commented 10 years ago

Crystal clear and very helpful, thank you Mr. Enrico M. Crisostomo.

I was learning linux regular expressions, and got stuck on -e "\.moon$" . I didn't know how to write logic NOT in the expression. My answer make me realise that I can use grep for further filtering.

Sorry, I'm a beginner of shell script. I will keep learning. And a big thank you for your kind help.

emcrisostomo commented 10 years ago

Thank you @yi. You're welcome, please feel free to post any issues, questions or suggestion you might have.