tail命令


tail 命令從指定點開始將檔案寫到標準輸出。使用tail命令的-f選項可以方便的查閱正在改變的紀錄檔檔案,tail -f filename會把filename裡最尾部的內容顯示在螢幕上,並且不但重新整理,以便看到最新的檔案內容。

1.命令格式

tail[必要引數][選擇引數][檔案]

2.命令功能

用於顯示指定檔案末尾內容,不指定檔案時,作為輸入資訊進行處理。常用檢視紀錄檔檔案。

3.命令引數

  • -f 迴圈讀取
  • -q 不顯示處理資訊
  • -v 顯示詳細的處理資訊
  • -c<數目> 顯示的位元組數
  • -n<行數> 顯示行數
  • --pid=PID-f合用,表示在進程ID,PID死掉之後結束。
  • -q, --quiet, --silent 從不輸出給出檔案名的首部。
  • -s, --sleep-interval=S-f合用,表示在每次反復的間隔休眠S秒

4.使用範例

範例1:顯示檔案末尾內容

命令:

tail -n 5 log.log

演示執行及輸出:

[yiibai@localhost test]$ tail -n 5 log.log
this is line 20.
this is line 21.
this is line 22.

-----------------end
[yiibai@localhost test]$

說明:顯示檔案最後5行內容

範例2:迴圈檢視檔案內容

命令:

tail -f ping.log

演示執行及輸出:

[yiibai@localhost test]$ ping www.baidu.com > ping.log &
[1] 2382
[yiibai@localhost test]$ tail -f ping.log
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=9 ttl=57 time=13.1 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=10 ttl=57 time=14.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=11 ttl=57 time=13.0 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=12 ttl=57 time=16.0 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=13 ttl=57 time=12.9 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=14 ttl=57 time=14.5 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=15 ttl=57 time=13.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=16 ttl=57 time=13.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=17 ttl=57 time=12.9 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=18 ttl=57 time=13.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=19 ttl=57 time=12.8 ms

64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=20 ttl=57 time=12.4 ms

64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=21 ttl=57 time=11.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=22 ttl=57 time=13.1 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=23 ttl=57 time=12.5 ms
^C
[yiibai@localhost test]$

說明:ping www.baidu.com > ping.log &,在後台ping遠端主機。並輸出檔案到ping.log;這種做法也使用於一個以上的檔案監視。用Ctrl+c來終止。

範例3:從第10行開始顯示檔案

命令:

tail -n +10 log.log

演示執行及輸出:

[yiibai@localhost test]$ tail -n +10 log.log
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
this is line 22.

-----------------end
[yiibai@localhost test]$