fswatchは、指定されたファイルやディレクトリの内容が変更されたときに通知アラートを取得する、クロスプラットフォームのファイル変更監視ツールです。
それは、異なるオペレーティングシステムで4種類の監視を実行します:
- A monitor build on the File System Events API of Apple OS X.
- A monitor based on kqueue, a notification interface present in FreeBSD 4.1 also supported on many *BSD systems, OS X inclusive.
- A monitor based on File Events Notification API of the Solaris kernel plus its spin-offs.
- A monitor based on inotify, a kernel subsystem that shows file system modifications to apps.
- A monitor based on ReadDirectoryChangesW, a Windows API that records alters to a directory.
- A monitor that regularly check that status of file system, keeps file modification times in memory, and manually determine file system changes (which works anywhere, where stat can be used).
fswatchの特徴
- いくつかのOS固有のAPIをサポート
- 再帰的なディレクトリ監視を許可
- 含めると除外する正規表現を使用したパスフィルタリングを実行
- カスタマイズ可能なレコード形式をサポート
- さらに、周期的なアイドルイベントをサポートします
Linuxシステムにfswatchをインストールする方法
残念ながら、fswatchパッケージは、任意のLinuxディストリビューションのデフォルトシステムリポジトリからインストールできません。最新バージョンのfswatchをインストールする唯一の方法は、次のインストール手順に示すように、ソースターボールからビルドすることです。
まず、次のwgetコマンドを使用して最新のfswatchターボールを取得し、次のようにインストールします:
$ wget https://github.com/emcrisostomo/fswatch/releases/download/1.9.3/fswatch-1.9.3.tar.gz $ tar -xvzf fswatch-1.9.3.tar.gz $ cd fswatch-1.9.3 $ ./configure $ make $ sudo make install
重要: fswatchをソースからコンパイルする前に、GNU GCC(CおよびC++コンパイラ)と開発ツール(Debian/Ubuntuではbuild-essential)がシステムにインストールされていることを確認してください。インストールされていない場合は、各々のLinuxディストリビューションで次のコマンドを使用してインストールしてください。
# yum group install 'Development Tools' [On CentOS/RHEL] # dnf group install 'Development Tools' [On Fedora 22+ Versions] $ sudo apt-get install build-essential [On Debian/Ubuntu Versions]
Debian/Ubuntuディストリビューションでは、fswatchコマンドを実行する際に次のエラーが発生する可能性があります。
fswatch: error while loading shared libraries: libfswatch.so.6: cannot open shared object file: No such file or directory
これを修正するには、以下のコマンドを実行する必要があります。これにより、
$ sudo ldconfig
リンクとキャッシュが動的ライブラリにリフレッシュされ、fswatchを使用する準備が整います。
Linuxでfswatchを使用する方法は?
$ fswatch [option] [path]
fswatchを実行する一般的な構文は次のとおりです。Linuxでは、デフォルトのinotifyモニターを使用することをお勧めします。利用可能なモニターをリストするには、-M
または- list-monitors
オプションを使用します。
$ fswatch -M $ fswatch --list-monitors

以下のコマンドは、現在のディレクトリ(/home/tecmint
)の変更を監視し、イベントを標準出力に4秒ごとに配信します。
-l
または--latency
オプションを使用すると、遅延を秒単位で設定できます。デフォルトは1秒です。
$ fswatch -l 4 .
次のコマンドは、/var/log/auth.logファイルの変更を5秒ごとに監視します。
$ fswatch -l 5 /var/log/auth.log
-t
または--timestamp
オプションを使用すると、各イベントのタイムスタンプが表示されます。UTC形式で時刻を印刷するには、-u
または--utf-time
オプションを使用します。時刻をフォーマットするには-f
または--format-time
フォーマットオプションを使用できます。
$ fswatch --timestamp /var/log/auth.log
次に、-x
または--event-flags
はfswatchにイベントフラグをイベントパスと一緒に表示するよう指示します。特定のセパレータを使用してイベントを印刷するには–event-field-separatorオプションを使用できます。
$ fswatch --events-flags ~ /var/log/auth.log
ホームディレクトリと/var/log/auth.logファイルでの変更を示すイベントの数値を表示するには、次のように-n
または--numeric
オプションを使用します:
$ fswatch --numeric ~ /var/log/auth.log
詳細な使用オプションと情報については、fswatchのmanページを参照してください:
$ man fswatch
詳細な情報や使用方法については、fswatchのGitHubリポジトリを訪問してください:https://github.com/emcrisostomo/fswatch
この投稿では、指定したファイルやディレクトリ階層の内容が変更されたときにLinuxユーザーに通知を送るためのシンプルなコマンドラインユーティリティについて説明しました。
I hope all went well with the installation, if that is not the case for you, make an effort to reach us via the feedback form below. In addition, in case you have used it before, you may want to offer us some thoughts about your experience with fswatch.
Source:
https://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/