Mac には「watch」や「inotifywait」のようなコマンドがありますか? 質問する

Mac には「watch」や「inotifywait」のようなコマンドがありますか? 質問する

Mac 上のフォルダーを監視し、監視対象のディレクトリに移動または作成されたファイル/フォルダーの名前を渡して bash スクリプトを実行したいと考えています。

ベストアンサー1

フスウォッチ

fswatchMac OS X FSEvents APIを使用してディレクトリを監視する小さなプログラムです。ディレクトリの変更に関するイベントを受信すると、指定されたシェルコマンドが実行されます。/bin/bash

GNU/Linuxをお使いの場合は、inotifywatch(inotify-toolsほとんどのディストリビューションのパッケージの一部) は同様の機能を提供します。

更新: fswatch BSD、Debian、Windows を含む多くのプラットフォームで使用できるようになりました。

構文 / 簡単な例

複数のパスを監視できる新しい方法 -バージョン 1.x 以降の場合:

fswatch -o ~/path/to/watch | xargs -n1 -I{} ~/script/to/run/when/files/change.sh

注: によって出力される番号は、でない場合はコマンド-oの末尾に追加されます。その番号を使用する場合は、コマンド内の任意の場所に を配置します。xargs-I{}{}

バージョン 0.xの古い方法:

fswatch ~/path/to/watch ~/script/to/run/when/files/change.sh

Homebrewによるインストール

2013年9月12日現在、自家製- やった!それでは、数式リスト ( brew update) を更新して、あとは次の手順に従います。

brew install fswatch

Homebrewを使わないインストール

次のコマンドを入力しますTerminal.app

cd /tmp
git clone https://github.com/alandipert/fswatch
cd fswatch/
make
cp fswatch /usr/local/bin/fswatch

システムにコンパイラがない場合、cXcodeまたはXcodeコマンドラインツールをインストールする必要があります(どちらも無料です)。ただし、その場合は、自作をチェック

fswatchバージョン 1.xの追加オプション

Usage:
fswatch [OPTION] ... path ...

Options:
 -0, --print0          Use the ASCII NUL character (0) as line separator.
 -1, --one-event       Exit fsw after the first set of events is received.
 -e, --exclude=REGEX   Exclude paths matching REGEX.
 -E, --extended        Use exended regular expressions.
 -f, --format-time     Print the event time using the specified format.
 -h, --help            Show this message.
 -i, --insensitive     Use case insensitive regular expressions.
 -k, --kqueue          Use the kqueue monitor.
 -l, --latency=DOUBLE  Set the latency.
 -L, --follow-links    Follow symbolic links.
 -n, --numeric         Print a numeric event mask.
 -o, --one-per-batch   Print a single message with the number of change events.
                       in the current batch.
 -p, --poll            Use the poll monitor.
 -r, --recursive       Recurse subdirectories.
 -t, --timestamp       Print the event timestamp.
 -u, --utc-time        Print the event time as UTC time.
 -v, --verbose         Print verbose output.
 -x, --event-flags     Print the event flags.

See the man page for more information.

おすすめ記事