fswatch 是一个跨平台的文件变更监视器,当指定文件或目录的内容被改变或修改时,会收到通知警报。
它在不同操作系统上执行四种类型的监视,例如:
- 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 的特点
- 支持多个特定于操作系统的 API
- 允许递归目录监视
- 使用包括和排除正则表达式执行路径过滤
- 支持可定制的记录格式
- 此外,它支持定期的空闲事件
如何在 Linux 系统中安装 fswatch
不幸的是,默认系统存储库中没有 fswatch 包可供任何 Linux 发行版安装。安装最新版本的 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++ 编译器)和开发工具(在 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
要修复它,您需要执行下面的命令,这将帮助刷新链接和缓存到动态库,然后您就可以开始使用fswatch了。
$ sudo ldconfig
如何在Linux上使用fswatch?
运行fswatch的一般语法是:
$ fswatch [option] [path]
在Linux上,建议您使用默认的inotify监视器,您可以通过使用-M或-list-monitors选项列出可用的监视器:
$ fswatch -M $ fswatch --list-monitors

下面的命令使您可以监视当前目录(/home/tecmint)中的更改,事件将每4秒传递到标准输出。
-l或-latitude选项允许您设置以秒为单位的延迟,默认为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-seperator选项使用特定分隔符打印事件。
$ 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/