天天看點

linux指令: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 log2014.log

輸出:

[root@localhost test]# tail -n 5 log2014.log

2014-09

2014-10

2014-11

2014-12

==============================[root@localhost test]#

說明:

顯示檔案最後5行内容

執行個體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

==============================

[root@localhost test]# tail -n +5 log2014.log

繼續閱讀