![tail -f 資料夾中最後修改的文件](https://rvso.com/image/1585798/tail%20-f%20%E8%B3%87%E6%96%99%E5%A4%BE%E4%B8%AD%E6%9C%80%E5%BE%8C%E4%BF%AE%E6%94%B9%E7%9A%84%E6%96%87%E4%BB%B6.png)
我正在嘗試使用tail -f
追蹤指定資料夾中的登入檔案。我可以使用簡單的命令來完成此操作tail -f [path to file]
,但是有沒有一種方法可以讓我有一個命令來運行並追蹤資料夾中最近/最後修改的檔案。無論如何都不是該領域的專家,因此非常感謝您的幫助
答案1
您可以運行ls -tp | sort | grep -v / | tail -n 1
命令並將它們通過for
循環,然後僅tail -F
針對最新文件執行命令以監視其內容變更。您也可以考慮使用ls -tpr | grep -v / | tail -n 1
指令。
for VAR in $(ls -tp | sort | grep -v / | tail -n 1); do tail -F $VAR; done
或者
for VAR in $(ls -tpr | grep -v / | tail -n 1); do tail -F $VAR; done
更多資源
-
-t sort by modification time, newest first -p, --indicator-style=slash append / indicator to directories -r, --reverse reverse order while sorting
-
-v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX .)
-
-n, --lines=[+]NUM output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM
答案2
你可以使用 多尾,在許多發行版上作為標準包提供。
範例 1:在一個視窗中合併兩個不同顏色的日誌文件
multitail -ci green /var/log/yum.log -ci yellow -I /var/log/mysqld.log
範例 2:顯示五個日誌文件,將兩個合併在一列中
multitail -s 2 -sn 1,3 /var/log/mysqld.log -I /var/log/xferlog /var/log/monitorix /var/log/ajenti.log /var/log/yum.log