Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Run A Command When A File Changes

[] Look into [TODO: Code shorthand span ] when it's not a background process to speed things up

[] Look at : [TODO: Code shorthand span ] Updated to see if that helps remove duplicate fires

Running this on the command line will execute a command whenever a file changes in the current directory :

bash
fswatch -o -r . | xargs -I{} echo "do something here"

I use it during development to trigger build scripts when source files change.

It can be limited to specific file extensions like this :

bash
fswatch -e ".*" -i "\.css$" -i "\.html$" . \
    | xargs -I{} echo "do something here"

Using the regex will watch for new files (which doesn't happen if you just do [TODO: Code shorthand span ] for example)

Details - First Example

  • [TODO: Code shorthand span ] - this is the command that does the watching. Instructions for installing it on Mac, Windows, and *nix are on the [[https : //emcrisostomo.github.io/fswatch/][fswatch site]]

  • [TODO: Code shorthand span ] - (lowercase letter "o") send only one signal per batch of changes [TODO: Code shorthand span ] sees

  • [TODO: Code shorthand span ] - sets the process to be recursive so files in sub - directories are watched as well

  • [TODO: Code shorthand span ] - tells the process to start watching in the current directory. This could also be another path like [TODO: Code shorthand span ]

  • [TODO: Code shorthand span ] - pipes the output of [TODO: Code shorthand span ] to [TODO: Code shorthand span ]

  • [TODO: Code shorthand span ] - receives the signal from [TODO: Code shorthand span ] and executes commands based on it's parameters

  • [TODO: Code shorthand span ] - tells [TODO: Code shorthand span ] to replace any occurrence of the string [TODO: Code shorthand span ] with the value that it received from [TODO: Code shorthand span ] . Without this, [TODO: Code shorthand span ] would pipe the output from [TODO: Code shorthand span ] directly to the [TODO: Code shorthand span ] command. That's useful in some instances, but not what I'm looking for in most of the time

  • [TODO: Code shorthand span ] - The command that gets run when [TODO: Code shorthand span ] receives a signal from [TODO: Code shorthand span ] . Switch this out with whatever you need to have run. My main use case is to replace this with a script to automatically build whatever it is that I'm working on

Details - Second Example

  • This example uses regular expressions to filter that paths that [TODO: Code shorthand span ] keeps an eye on

  • The example is split to multiple lines with the [TODO: Code shorthand span ] at the end of the first line. I'm doing that to help get everything visible for the sample. In practice, I run everything on one line like the first example

  • The methodology is to start by excluding everything with [TODO: Code shorthand span ] and then setting up the patters to include via the [TODO: Code shorthand span ] flag

  • You can setup multiple include statements to watch for multiple patters (e.g. this example which watching for both [TODO: Code shorthand span ] and [TODO: Code shorthand span ] files)

  • The [TODO: Code shorthand span ] flag does not appear to be necessary when using regular expressions like this

- I use [TODO: Code shorthand span ] because the way files are saved can result in multiple triggers for a single save. I'm generally looking for a single signal for each time I press save. This helps reduce the number of multiple triggers, but does not eliminate it

- The value that's sent when the [TODO: Code shorthand span ] flag is in place is the number of paths that changed in the batch

- Removing [TODO: Code shorthand span ] will send one message for every change it sees with the path that changed

- In [[https : //stackoverflow.com/a/37237681][this StackOverflow answer]] some folks mention problems with the regex matching in 2019 - 2020. It's unclear if that was resolved

- My original notes included running the commands from [TODO: Code shorthand span ] like this [TODO: Code shorthand span ] . The [TODO: Code shorthand span ] flag on bash is to use it as if there's a login shell and the [TODO: Code shorthand span ] flag is how you pass in a command. The specific command was [TODO: Code shorthand span ] which would send a command to a vagrant instance. I haven't checked if that can be done with the [TODO: Code shorthand span ] approach. Leaving that here as note in case it turns out to be useful

- Check out [[https : //emcrisostomo.github.io/fswatch/doc/][the official docs]] if you need more info

- I also have this in my notes for a multi - line approach in a script. Need to check it out a little more and write it up after verifying the functionality

bash
#!/bin/bash

fswatch -0 . | while read -d "" event
do 
    echo $event
done