フォルダー内の最後に変更されたファイルを tail -f する

フォルダー内の最後に変更されたファイルを tail -f する

tail -f指定したフォルダ内のログイン ファイルを追跡するために使用しようとしています。簡単なコマンドでこれを行うtail -f [path to file]ことができますが、フォルダ内の最新/最終変更ファイルを実行して追跡するコマンドを使用する方法はありますか。この分野の専門家ではないので、助けていただければ幸いです。

答え1

コマンドを実行してls -tp | sort | grep -v / | tail -n 1forループして、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: 2 つのログファイルを異なる色で 1 つのウィンドウにマージする

multitail -ci green /var/log/yum.log -ci yellow -I /var/log/mysqld.log

ここに画像の説明を入力してください

例 2: 5 つのログファイルを表示し、2 つを 1 つの列にマージする

 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 ターミナルで複数のファイルを同時に監視

関連情報