7

To monitor a file in linux, I can use inotify-tools like this

#!/bin/bash

# with inotify-tools installed, watch for modification of file passed as first param
while inotifywait -e modify $1; do
        # do something here
done

but how would I achieve this in OSX?

Billy Moon
  • 57,113
  • 24
  • 136
  • 237

3 Answers3

6

If you want to wrap this into a Python script, you can use Watchdog, which works with both Linux and OSX.

https://pypi.python.org/pypi/watchdog

Here is what it looks like to replace pyinotify with watchdog:

https://github.com/raphdg/baboon/commit/2c115da63dac16d0fbdc9b45067d0ab0960143ed

Watchdog also has a shell utility called watchmedo:

watchmedo shell-command \
    --patterns="*.py;*.txt" \
    --recursive \
    --command='echo "${watch_src_path}"' \
    .
Amir Nissim
  • 3,173
  • 2
  • 24
  • 19
Peter Sankauskas
  • 2,882
  • 4
  • 27
  • 28
3

Yes, you can use the FSEvents API

kenorb
  • 155,785
  • 88
  • 678
  • 743
Baversjo
  • 3,656
  • 3
  • 34
  • 47
1

You can use entr tool. Example usage:

ls some_file | entr do_something

On Mac install via Brew: brew install entr.

kenorb
  • 155,785
  • 88
  • 678
  • 743