linux 下 fswatch 的使用

linux 下 fswatch 的使用

1. fswatch 的安装:
$sudo apt install fswatch

当然也可以用源码安装.

2. fswatch 的使用.
fswatch 是一个应用程序,可以直接使用它,也可以写程序调用libfswatch 库,
完成当被监视的文件发生期望的事件时,回调自己的函数等。

我阅读了它的源码,因而对使用有了一定的了解. 下面拣最有用的说.

例如: 监视 /tmp 目录发生的事件

$fswatch -t -x /tmp
 

其中
-t 打印时间
-x 打印发生的事件, 这个选项很关键!它能够显示发生了何种事件,包括创建,修改,删除,文件,目录,特定平台相关,属主改变,属性改变等等事件.
/tmp 是被监视的目录 /tmp 目录(该目录下所有操作均被监视)


fswatch -h 是帮助信息. 不过不认真阅读源码,帮助信息有点多而杂的感觉.
fswatch -l 可设置延时,即多长时间执行一次查询,默认1.0秒
fswatch -M 查看可使用的monitor, linux 下默认使用inotify_monitor


ok, 这就够了!

实际使用时,往往还需要使用-r 选项, 递归监视。

还可以指定event 过滤器,例如 --event=Created , 只打印Created 事件, 具体事件可man fswatch

举例: 监视当ip地址改变时,到底改变了什么文件. 我看到了如下结果!

/etc/NetworkManager$ fswatch -r --event=Created -x ./
/etc/NetworkManager/system-connections/Wired connection 1.nmconnection.N4B0X1 Created
/etc/NetworkManager/system-connections/Wired connection 1.nmconnection Created
/etc/NetworkManager/system-connections/Wired connection 1.nmconnection.U82GX1 Created
/etc/NetworkManager/system-connections/Wired connection 1.nmconnection Created

关于event-type , pc 机附带的手册页中没有介绍, 从官网说明书上摘录如下:供参考

Event Types
Event flags identify the kind of change a file system object has undergone. Many of them directly map to common file system operations (such as creation, deletion, update, etc.), others are less common (such as attribute modification), and others are monitor and platform specific.

Currently, fswatch maps monitor-specific event flags to 'global' event flags acting as a sort of 'greatest common denominator' of all the available monitor flags. The following global event flags are available::

NoOp
Idle event, optionally issued when no changes were detected.

PlatformSpecific
This event maps a platform-specific event that has no corresponding flag.

Created
The object has been created.

Updated
The object has been updated. The kind of update is monitor-dependent.

Removed
The object has been removed.

Renamed
The object has been renamed.

OwnerModified
The object’s owner has changed.

AttributeModified
An object’s attribute has changed.

MovedFrom
The object has moved from this location to a new location of the same file system.

MovedTo
The object has moved from another location in the same file system into this location.

IsFile
The object is a regular file.

IsDir
The object is a directory.

IsSymLink
The object is a symbolic link.

Link
The object link count has changed.

Overflow
The monitor has overflow

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值