inotify 是 Linux 內核提供的一種文件系統事件監控機制,它可以實時監控文件或目錄的變化,如創建、刪除、修改等。在 Debian 系統中,你可以使用 inotify-tools 包來實現 inotify 的功能。
以下是在 Debian 中安裝和使用 inotify-tools 的步驟:
安裝 inotify-tools
打開終端,運行以下命令來安裝 inotify-tools:
sudo apt update
sudo apt install inotify-tools
使用 inotifywait 命令
inotifywait 是 inotify-tools 包中的一個命令行工具,用于監控文件系統事件。以下是一些基本用法示例:
監控單個文件的變化:
inotifywait -m /path/to/file
監控目錄及其子目錄中的所有文件變化:
inotifywait -m -r /path/to/directory
監控特定類型的事件(如創建、刪除、修改):
inotifywait -m -e create,delete,modify /path/to/directory
監控多個事件:
inotifywait -m -e create,delete,modify --format '%w%f %e' /path/to/directory
設置監控的超時時間(單位:秒):
inotifywait -t 60 -m /path/to/directory
在監控過程中執行自定義腳本:
inotifywait -m -e create,delete,modify --format '%w%f %e' /path/to/directory | while read path event; do
# 在這里執行你的自定義腳本
echo "File $path was $event"
done
通過這些步驟,你可以在 Debian 系統中使用 inotify 來監控文件系統的變化。根據你的需求,你可以調整命令參數以實現更復雜的監控功能。