A010 tail 命令詳解 日誌檔案

2021-06-25 09:24:28 字數 3031 閱讀 8074

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 log2014.log

輸出:

[root@localhost test]

#tail -n 5 log2014.log 

2014-09

2014-10

2014-11

2014-12

******************************

[root

@localhost test]#

說明:

顯示檔案最後5行內容

例項1.2:顯示資料夾中最後的幾個檔案

命令:

ls -tl|tail -n 

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

命令:

tail -f test.log

輸出:

[root@localhost ~]

#ping 192.168.120.204 > test.log &

[1] 11891

[root

@localhost ~]

#tail -f test.log 

ping 192.168.120.204 (192.168.120.204) 56(84

) bytes of data.

64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms

64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms

64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms

64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms

64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms

64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms

64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms

64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms

[root

@localhost ~]#

說明:

ping 192.168.120.204 > test.log & //在後台ping遠端主機。並輸出檔案到test.log;

這種做法也使用於乙個以上的檔案監視。用ctrl+c來終止。

例項3:從第5行開始顯示檔案

命令:

tail -n +5 log2014.log

輸出:

[root@localhost test]

#cat log2014.log 

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

******************************

[root

@localhost test]

#tail -n +5 log2014.log

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

******************************

參考:  

tail 命令詳解

tail 命令詳解 功能 顯示指定檔案的後若干行。語法 tail num 引數 檔案 tail 命令中各個選項的含義為 num 從第num行以後開始顯示。num 從距檔案尾num行處開始顯示。如果省略num引數,系統預設值為10。l 以文字行為num的計數單位。與引數選項 num或 num選項同時使...

tail 命令詳解

功能 顯示指定檔案的後若干行。語法 tail num 引數 檔案 tail命令中各個選項的含義為 num 從第num行以後開始顯示。num 從距檔案尾num行處開始顯示。如果省略num引數,系統預設值為10。l 以文字行為num的計數單位。與引數選項 num或 num選項同時使用時,num表示要顯示...

tail命令詳解

tail命令 用途 顯示檔案的最後幾行。標準語法 tail f c number n number m number b number k number file 逆序顯示行 tail r n number file 描述 tail命令從指定點開始將file引數指定的檔案寫到標準輸出。如果沒有指定檔...