20

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:

Platform:

  • MacOS 10.9.5.
Community
  • 1
  • 1
Peter Grill
  • 548
  • 7
  • 23
  • According to [What topics can I ask about here](https://stackoverflow.com/help/on-topic) (stack overflow): _a specific programming problem_, or _software tools commonly used by programmers_ and _a practical, answerable problem that is unique to software development_. Seems to me that this question meets those requirements. I don't need the question opened, but seems odd to me that it was closed. – Peter Grill Oct 02 '22 at 06:00

2 Answers2

58

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.

Enrico M. Crisostomo
  • 1,653
  • 1
  • 17
  • 26
  • 9
    this isn't working for me on linux, I get no events at all. I could create a detailed issue on your github repo, but every issue since march has 0 replies. – Guido Tarsia Sep 09 '19 at 19:32
  • @erandros I'm also experiencing similar issues (at first no output, soon after some output which is not reliable). Using a different monitor (`-m poll_monitor`) it seems to work fine. – Cornflex Sep 11 '19 at 13:22
  • I can't get this to work with Linux (Xubuntu 20.04) running in VirtualBox. Weird thing, I get "Invalid monitor name" when I specify `-m inotify`. Without filters everything works as expected – mozey Jun 12 '20 at 16:18
  • This is a timesaver. Thank you. I had guessed including only a single filetype involved excluding every other file type, but I wouldn't have known to double up on backslashes. – LumpyGrads Jun 23 '20 at 06:27
  • @erandros did you found the solution for linux? – Dipesh KC Jun 23 '20 at 17:23
  • @mozey did you found the solution for linux? – Dipesh KC Jun 23 '20 at 17:24
  • @DipeshKC I couldn't get the filters working how I wanted on Linux. So I wrote my own watcher that is a wrapper around golang fsnotity lib, see here https://github.com/mozey/watcher – mozey Jun 24 '20 at 12:02
5

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"
Ivar Refsdal
  • 85
  • 1
  • 5
  • How would someone modify this to look for multiple file extensions? – David Alsh Sep 25 '19 at 07:42
  • 2
    You can just add the `-i` parameter multiple times, e.g., `fswatch -0 -o -e '.*' -i '.*/*\.xxx$' -i '.*/*\.yyy' . | xargs -0 -n1 -I{} bash -c 'echo {}'` – f0xdx Nov 18 '22 at 17:13