tail -f 資料夾中最後修改的文件

tail -f 資料夾中最後修改的文件

我正在嘗試使用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

更多資源

  • ls

       -t     sort by modification time, newest first
    
       -p, --indicator-style=slash
              append / indicator to directories
    
       -r, --reverse
              reverse order while sorting
    
  • sort

  • grep

    -v, --invert-match
    Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX .)
    
  • tail

      -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

在此輸入影像描述

來源: MultiTail – 在單一 Linux 終端機中同時監控多個文件

相關內容