fswatch – 监视Linux中的文件和目录更改或修改

fswatch是一款跨平台的文件变更监视器,当指定文件或目录的内容被更改或修改时,它会发出通知警报。

它在不同操作系统上执行四种类型的监视器,如下:

  1. A monitor build on the File System Events API of Apple OS X.
  2. A monitor based on kqueue, a notification interface present in FreeBSD 4.1 also supported on many *BSD systems, OS X inclusive.
  3. A monitor based on File Events Notification API of the Solaris kernel plus its spin-offs.
  4. A monitor based on inotify, a kernel subsystem that shows file system modifications to apps.
  5. A monitor based on ReadDirectoryChangesW, a Windows API that records alters to a directory.
  6. 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的特点

  1. 支持多个特定于操作系统的API
  2. 允许递归目录监视
  3. 使用包括和排除正则表达式进行路径过滤
  4. 支持可定制的记录格式
  5. 此外,它支持定期的空闲事件

在Linux系统中安装fswatch的方法

不幸的是,默认情况下,任何Linux发行版的系统存储库中都无法安装fswatch包。安装fswatch的最新版本的唯一方法是按照以下安装说明从源代码tarball构建。

首先使用以下wget命令获取最新的fswatch tarball,并按照以下安装它的步骤:

$ 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 ++编译器)和开发工具(build-essentialDebian/Ubuntu上)。如果没有,请使用以下命令在您各自的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

為了解決這個問題,您需要執行以下命令,這將有助於在您開始使用fswatch之前刷新鏈接和緩存到動態庫。

$ sudo ldconfig

如何在Linux上使用fswatch?

運行fswatch的一般語法為:

$ fswatch [option] [path]

在Linux上,建議您使用默認的inotify監視器,您可以通過使用-M或-list-monitors選項列出可用的監視器:

$ fswatch -M
$ fswatch --list-monitors
fswatch – List Monitors

下面的命令使您能夠監視當前目錄(/home/tecmint)中的更改,事件每4秒鐘傳送到標準輸出。

使用-l或-latency選項可以設置延遲時間(以秒為單位),默認值為1秒。

$ fswatch -l 4 . 

下一個命令每5秒監視/var/log/auth.log文件的更改:

$ 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 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/