Linux文件监控工具——inotify-tools

1. 安装inotify-tools:

sudo apt-get install inotify-tools

2. inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。


inotifywait

语法:inotifywait [-hcmrq] [-e] [-t] [--format] [--timefmt] file1 [file2] [file3] [...]

参数:

-h|--help
Show this help text.
显示帮助信息

@<file>
Exclude the specified file from being watched.
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。

--exclude <pattern>
Exclude all events on files matching the extended regular expression <pattern>.
正则匹配需要排除的文件,大小写敏感。

--excludei <pattern>
Like --exclude but case insensitive.
正则匹配需要排除的文件,忽略大小写。

-m|--monitor
Keep listening for events forever.  Without this option, inotifywait will exit after one event is received.
接收到一个事件而不退出,无限期地执行。默认的行为是接收到一个事件后立即退出。


-d|--daemon
Same as --monitor, except run in the background logging events to a file specified by --outfile.Implies --syslog.
跟–-monitor一样,除了是在后台运行,需要指定-–outfile把事件输出到一个文件。也意味着使用了–syslog。

-r|--recursive
Watch directories recursively.
监视一个目录下的所有子目录。

--fromfile <file>
Read files to watch from <file> or '-' for stdin.
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头;'-'表示标准输入。


-o|--outfile <file>
Print events to <file> rather than stdout.
输出事件到一个文件而不是标准输出。

-s|--syslog
Send errors to syslog rather than stderr.
输出错误信息到系统日志

-q|--quiet
Print less (only print events).
不会输出详细信息

-qq
Print nothing (not even events).
除了致命错误,不会输出任何信息。

--format <fmt>
Print using a specified printf-like format string; read the man page for more details.
指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式

--timefmt <fmt>
指定时间格式,用于–-format选项中的%T格式。
strftime-compatible format string for use with %T in --format string.

-c|--csv
Print events in CSV format.
输出csv格式

-t|--timeout <seconds>
When listening for a single event, time out after waiting for an event for <seconds> seconds. If <seconds> is 0, inotifywait will never time out.
设置超时时间,如果为0,则无限期地执行下去。

-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s).  If omitted, all events are listened for.
指定监视的事件。


可监听事件:

access 文件读取
modify 文件更改。
attrib 文件属性更改,如权限,时间戳等。
close_write 以可写模式打开的文件被关闭,不代表此文件一定已经写入数据。
close_nowrite 以只读模式打开的文件被关闭。
close 文件被关闭,不管它是如何打开的。
open 文件打开。
moved_to 一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发。
moved_from 一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发。
move 包括moved_to和 moved_from
move_self 文件或目录被移除,之后不再监听此文件或目录。
create 文件或目录创建
delete 文件或目录删除
delete_self 文件或目录移除,之后不再监听此文件或目录
unmount 文件系统取消挂载,之后不再监听此文件系统。


示例:

创建一个临时目录,并监听该目录:

huey2672@huey2672-VM:~$ mkdir tmpdir
huey2672@huey2672-VM:~$ inotifywait -rm tmpdir/
在另一终端,执行以下操作:

huey2672@huey2672-VM:~$ cd tmpdir/
huey2672@huey2672-VM:~/tmpdir$ touch f1.txt
huey2672@huey2672-VM:~/tmpdir$ mv f1.txt f2.txt
输出的监听信息:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
tmpdir/ CREATE f1.txt
tmpdir/ OPEN f1.txt
tmpdir/ CLOSE_WRITE,CLOSE f1.txt
tmpdir/ MOVED_FROM f1.txt
tmpdir/ MOVED_TO f2.txt

inotifywatch

语法:inotifywatch [-hvzrqf] [-e] [-t] [-a] [-d] file1 [file2] [file3] [...]
参数:

-h|--help
Show this help text.
输出帮助信息

-v|--verbose
Be verbose.
输出详细信息

@<file>
Exclude the specified file from being watched.
排除不需要监视的文件,可以是相对路径,也可以是绝对路径。

--fromfile <file>
Read files to watch from <file> or `-' for stdin.
从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。

--exclude <pattern>
Exclude all events on files matching the extended regular expression <pattern>.
正则匹配需要排除的文件,大小写敏感。

--excludei <pattern>
Like --exclude but case insensitive.
正则匹配需要排除的文件,忽略大小写

-z|--zero
In the final table of results, output rows and columns even if they consist only of zeros (the default is to not output these rows and columns).
输出表格的行和列,即使元素为空

-r|--recursive
Watch directories recursively.
监视一个目录下的所有子目录。

-t|--timeout <seconds>
Listen only for specified amount of time in seconds; if omitted or 0, inotifywatch will execute until receiving an interrupt signal.
设置超时时间

-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s).  If omitted, all events are listened for.
只监听指定的事件

-a|--ascending <event>
Sort ascending by a particular event, or 'total'.
以指定事件升序排列。

-d|--descending <event>
Sort descending by a particular event, or 'total'.
以指定事件降序排列。


可监听事件:

(于inotifywait一样)


示例:

创建一个目录,再创建一个文件于该目录,并监听该目录:

huey2672@huey2672-VM:~$ mkdir tmpdir
huey2672@huey2672-VM:~$ touch tmpdir/foo.txt
huey2672@huey2672-VM:~$ inotifywatch -v -e access -e modify -t 60 -r tmpdir/
Establishing watches...
Setting up watch(es) on tmpdir/
OK, tmpdir/ is now being watched.
Total of 1 watches.
Finished establishing watches, now collecting statistics.
Will listen for events for 60 seconds.
在另一终端,执行一下操作:
huey2672@huey2672-VM:~$ date > tmpdir/foo.txt 
huey2672@huey2672-VM:~$ cat tmpdir/foo.txt
输出的监听信息:

total  access  modify  filename
3      1       2       tmpdir/



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值