天天看點

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 檔案系統取消挂載,之後不再監聽此檔案系統。

示例:

建立一個臨時目錄,并監聽該目錄:

[email protected]:~$ mkdir tmpdir
[email protected]:~$ inotifywait -rm tmpdir/
           

在另一終端,執行以下操作:

[email protected]:~$ cd tmpdir/
[email protected]:~/tmpdir$ touch f1.txt
[email protected]:~/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一樣)

示例:

建立一個目錄,再建立一個檔案于該目錄,并監聽該目錄:

[email protected]:~$ mkdir tmpdir
[email protected]:~$ touch tmpdir/foo.txt
[email protected]:~$ 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.
           

在另一終端,執行一下操作:

[email protected]:~$ date > tmpdir/foo.txt 
[email protected]:~$ cat tmpdir/foo.txt
           

輸出的監聽資訊:

total  access  modify  filename
3      1       2       tmpdir/
           

繼續閱讀