inotifywait To run command based on type of file modified

3.4k views Asked by At

I'm specifically trying to simultaneously watch 2 different filetypes in 2 subdirectories:

  .coffee filetypes in the 'js' subdirectory
  .styl filetypes in the 'css' subdirectory

I only know so much bash and I'm trying to get something like this to function

while inotifywait --format %w%f ./{css,js}/*.{styl,coffee}; do
  # regex pattern to match the file ending
  # and define it to $ending

  if[ $ending == coffee ]
  then
   coffee -c $file
  elif[ $ending == styl ]
  then
    stylus -c $file
  fi
done



// Edit // modified this line:

while inotifywait --format %w%f ./{css,js}/*.{styl,coffee}; do

So now it checks both files but if there's no .coffee files on the css folder, or .styl files in js, it returns an error that no files were found.

Additionally I noticed when I run this script, it returns/runs the following 3 times

Setting up watches.
Watches established.
./css/styles.styl
1

There are 1 answers

0
Marius Miliunas On

Not the cleanest solution but works

#!/bin/sh

while file=$(inotifywait -r -e modify --format "%w%f" ./); do
  EXT=${file##*.}
  if [ $EXT = "styl" ]
  then
    stylus -c $file
  fi
  if [ $EXT = "coffee" ]
  then
    coffee -c $file
  fi
done

If you have a better solution that only watches the files I want, then I'm all ears