Bash

inotify:回顯目錄中哪個文件已更改

  • February 25, 2020

我剛剛學會瞭如何不斷檢查文件是否被修改

while inotifywait -q -e modify filename >/dev/null; do
   echo "filename is changed"
   # do whatever else you need to do
done

如果我使用目錄名而不是文件名,我可以檢查目錄的所有文件:

while inotifywait -q -e modify directoryname >/dev/null; do
   echo "filename is changed"
   # do whatever else you need to do
done

但是我怎樣才能知道echo已更改文件的文件名?

inotifywait在監視的目錄上發出一組連續的事件,因此推薦的方法是將監視移出while循環並在新循環中查找事件,該循環查看inotify. 請注意,這涉及刪除--quiet標誌,因為read命令需要查看這些事件並通過標準輸入讀取它。

inotifywait -m -e modify "directoryname" | 
while read -r dir action file; do
   echo "The file '$file' appeared in directory '$dir' via '$action'"
done

引用自:https://unix.stackexchange.com/questions/569650