inotify 是 Linux 內核提供的一種文件系統事件監控機制,它可以實時監控文件或目錄的變化,如創建、刪除、修改等。在 Ubuntu 中,你可以使用 inotify-tools 這個軟件包來方便地使用 inotify。
以下是在 Ubuntu 中配置和使用 inotify 的步驟:
sudo apt update
inotify-tools:sudo apt install inotify-tools
inotifywait 是 inotify-tools 中的一個命令行工具,用于監控文件系統事件。
inotifywait [選項] 目錄
-m 或 --monitor:持續監控目錄,而不是只運行一次。-r 或 --recursive:遞歸監控目錄及其子目錄。-e 或 --event:指定要監控的事件,如 create, delete, modify 等??梢灾付ǘ鄠€事件,用逗號分隔。-q 或 --quiet:減少輸出信息。-v 或 --verbose:增加輸出信息。監控當前目錄下的所有文件和子目錄的變化:
inotifywait -m -r -e create,delete,modify .
監控 /var/log 目錄下的所有文件的變化,并且每發生一個事件就輸出詳細信息:
inotifywait -m -r -e modify /var/log -v
inotifywatch 是另一個 inotify-tools 中的工具,用于統計文件系統事件的發生次數。
inotifywatch [選項] 目錄 [時間間隔]
-t 或 --timefmt:指定時間格式。-e 或 --event:指定要監控的事件。-m 或 --monitor:持續監控目錄。-r 或 --recursive:遞歸監控目錄及其子目錄。統計 /var/log 目錄下所有文件的變化次數,每 60 秒統計一次:
inotifywatch -m -r -e modify /var/log -t "%y-%m-%d %H:%M:%S" 60
通過這些工具和選項,你可以在 Ubuntu 中靈活地配置和使用 inotify 來監控文件系統的變化。