13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mac OS X で dnotify, inotify のような機能をお手軽に利用することはできないかしら?

Last updated at Posted at 2015-01-06

ディレクトリやファイルの監視を、コマンドでやりたい。

Mac OS X

こんな感じか。 ∥ osx - Is there a command like "watch" or "inotifywait" on the Mac? - Stack Overflow

fswatch(1) が Homebrew にある。

$ fswatch -0 /path/to/dir/ | xargs -0 -n 1 /bin/echo d:

fswatch(1), xargs(1) ともに、-0 オプションで NULL による行終端。xargs(1) は -n 1 で、一単位ごとにコマンド呼び出しをする。

何のシステムコールを用いているかは知らないが、たとえば Kobito のデータが格納されている ~/Library/Containers/com.qiita.Kobito/Data/Library/Kobito/ を watch していても、Kobito 稼働中は何も起きない。Save をしても無反応。Kobito を閉じる際に、データベースや WAL ファイルが close された時点で反応が出る。

これは良いやらどうやらと思ったが、大きなファイルを追記し続けている間に延々と反応されても良くないので、これは妥当なのかも知れない。

Linux では?

inotify-tools の inotifywait かな(%e: Event, %w: Watched file or directory, %f: ディレクトリのイベントであればファイル名、さもなければ空文字列)。

$ inotifywait -m /tmp/hoge

-0 オプションが無いので使いづらいな。ただ、イベントは詳細なので、自分で切り分ければ良い。こんな感じか。

String contains in bash - Stack Overflow

inotifywait --monitor --recursive --format "%e %w%f" /tmp/hoge |
  while read events path
  do
    case ,$events, in
      *,CLOSE,* )
        echo d: $events $path
        ;;
    esac
  done
13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?