Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fswatch to watch only a certain file extension [closed]

I am using fswatch and only want it triggered if a file with extension .xxx is modified/created etc. The documentation and the second reference below indicate that:

  • All paths are accepted by default, unless an exclusion filter says otherwise.
  • Inclusion filters may override any exclusion filter.
  • The order in the definition of filters in the command line has no effect.

Question: What is the regular expression to use to exclude all files that do not match the .xxx extension?

References:

  • Is there a command like "watch" or "inotifywait" on the Mac?
  • Watch for a specific filetype

Platform:

  • MacOS 10.9.5.
like image 797
Peter Grill Avatar asked Jan 11 '16 02:01

Peter Grill


2 Answers

I'm fswatch author. It may not be very intuitive, but fswatch includes everything unless an exclusion filter says otherwise. Coming to your problem: you want to include all files with a given extension. Rephrasing in term of exclusion and inclusion filters:

  • You want to exclude everything.
  • You want to include files with a given extension ext.

That is:

  • To exclude everything you can add an exclusion filter matching any string: .*.

  • To include files with a given extension ext, you add an inclusion filter matching any path ending with .ext: \\.ext$. In this case you need to escape the dot . to match the literal dot, then the extension ext and then matching the end of the path with $.

The final command is:

$ fswatch [options] -e ".*" -i "\\.ext$"

If you want case insensitive filters (e.g. to match eXt, Ext, etc.), just add the -I option.

like image 79
Enrico M. Crisostomo Avatar answered Sep 19 '22 19:09

Enrico M. Crisostomo


You may watch for changes to files of a single extension like this:

fswatch -e ".*" -i ".*/[^.]*\\.xxx$" .

This will exclude all files and then include all paths ending with .xxx (and also exclude files starting with a dot).

If you want to run a command on the file change, you may add the following:

fswatch -e ".*" -i ".*/[^.]*\\.xxx$" -0 . | xargs -0 -n 1 -I {} echo "File {} changed"
like image 42
Ivar Refsdal Avatar answered Sep 23 '22 19:09

Ivar Refsdal