【Linux基礎】tail命令查看日志


一、tail 命令介紹

tail 命令可以將文件指定位置到文件結束的內容寫到標准輸出。

如果你不知道tail命令怎樣使用,可以在命令行執行命令tail --help就能看到tail命令介紹和詳細的參數使用介紹,內容如下(我幫大家翻譯了一下)。

[root@yanggongzi ~]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
將每個文件的最后10行打印到標准輸出。
如果有多個文件,在每個文件之前都有一個給出文件名的頭文件。
沒有文件,或者當文件為-時,讀取標准輸入。

Mandatory arguments to long options are mandatory for short options too.
長選項必須用的參數在使用短選項時也是必須的。
  -c, --bytes=K            output the last K bytes;
                           or use -c +K to output bytes starting with the Kth of each file
                           輸出最后的 K 個字節;
			               或者使用 -c +K 從每個文件的第K字節開始打印。
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           an absent option argument means 'descriptor'
                           隨着文件的增長,輸出附加數據;(動態輸出最新的信息);
                           沒有選項參數意味着“描述符”
                           
  -F                       same as --follow=name --retry
                           與 --follow=name --retry 作用相同
                           
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output starting with the Kth
                           輸出最后的K行,而不是最后的10行;
                           或者使用-n +K從第K個開始輸出
                           
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files);
                           with inotify, this option is rarely useful
                           使用——follow=name,在N次(默認為5次)迭代后,重新打開一個大小沒有改變的文件,看看它是否被解除鏈接或重命名(這是旋轉日志文件的常見情況);
                           對於inotify,這個選項很少有用
                             
      --pid=PID            with -f, terminate after process ID, PID dies
                           與“-f”選項連用,當指定的進程號的進程終止后,自動退出tail命令

  -q, --quiet, --silent    never output headers giving file names
                           當有多個文件參數時,不輸出各個文件名
                           
      --retry              keep trying to open a file if it is inaccessible
                           即是在tail命令啟動時,文件不可訪問或者文件稍后變得不可訪問,都始終嘗試打開文件。使用此選項時需要與選項“——follow=name”連用

  -s, --sleep-interval=N   
                           with -f, sleep for approximately N seconds (default 1.0) between iterations;
                           with inotify and --pid=P, check process P at least once every N seconds
                           與“-f”選項連用,指定監視文件變化時間隔的秒數(默認為1.0);
			               使用inotify和-pid=P,每N秒檢查進程P至少一次
			               
  -v, --verbose            always output headers giving file names
                           當有多個文件參數時,總是輸出各個文件名
                           
      --help               display this help and exit
                           顯示此幫助信息並退出
                           
      --version            output version information and exit
                           顯示版本信息並退出

If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
如果K前面的字符(字節數或行數)是'+',每個文件從第K項開始打印,否則,打印文件中最后的K項。K可能有一個乘數后綴:b 512,kB 1000,K 1024,MB 1000 1000,M 1024 1024,GB 1000 1000,G 1024 1024 1024,等等,對於T,P,E,Z,y。

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail"ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.
使用——follow (-f), tail默認跟隨文件描述符,這意味着即使重命名了尾部文件,tail也將繼續跟蹤其尾部。
當您真正想要跟蹤文件的實際名稱而不是文件描述符(例如,日志旋轉)時,這種默認行為是不可取的。
在這種情況下使用——follow=name。這將導致tail以一種適合重命名、刪除和創建的方式跟蹤已命名文件。

二、tail 命令使用示例

1、輸出最后200個字符

tail -c 200 test.log

2、從第900個字符開始輸出,一直到最后

tail -c +900 test.log

3、輸出最后20行

tail -n 20 test.log

4、從第36行開始輸出,一直到最后

tail -n +36 test.log

5、輸出指定文件的最后十行,同時繼續監視文件內容有無變化,新增內容會繼續輸出,直到按下 [Ctrl-C] 組合鍵退出

tail -f test.log

6、指定多個文件並輸出文件名

tail -v test1.log test2.log

7、指定多個文件不輸出文件名

tail -q test1.log test2.log

三、tailf、tail -f、tail -F 的區別

  • tail -f
    等同於–follow=descriptor,根據文件描述符進行追蹤,當文件改名或被刪除,追蹤停止

  • tail -F
    等同於–follow=name --retry,根據文件名進行追蹤,並保持重試,即該文件被刪除或改名后,如果再次創建相同的文件名,會繼續追蹤

  • tailf
    等同於tail -f -n 10(貌似tail -f或-F默認也是打印最后10行,然后追蹤文件),與tail -f不同的是,如果文件不增長,它不會去訪問磁盤文件,所以tailf特別適合那些便攜機上跟蹤日志文件,因為它減少了磁盤訪問,可以省電

當我們設置了滾動日志時,需要持續實時監控最新的日志輸出,那么就要用tail -F,而不能用tailf 和 tail -f。因為當日志xxx.log達到了設定閾值重命名成了xxx01.log時,后兩個命令追蹤的還是xxx01.log文件而不是新創建的xxx.log文件,這時就不能繼續監控最新日志了。

四、常用快捷鍵

【Ctrl】+【S】 暫停刷新。
【Ctrl】+【Q】繼續刷新。
【Ctrl】+【C】退出 tail 命令。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM