linux每日命令(15):tail命令

tail 命令從指定點開始將文件寫到標準輸出.使用tail命令的-f選項可以方便的查閱正在改變的日誌文件,tail -f filename會把filename裏最尾部的內容顯示在屏幕上,並且不斷刷新,使你看到最新的文件內容.

一.命令格式;

tail [必要參數] [選擇參數] [文件]   

二.命令功能:

用於顯示指定文件末尾內容,不指定文件時,作爲輸入信息進行處理。常用查看日誌文件。

三.命令參數:

參數

描述

-f

循環讀取

-q

不顯示處理信息

-v

顯示詳細的處理信息

-c<數目>

顯示的字節數

-n<行數>

顯示行數

--pid=PID

與-f合用,表示在進程ID,PID死掉之後結束.

-q

--quiet, --silent 從不輸出給出文件名的首部

-s

--sleep-interval=S 與-f合用,表示在每次反覆的間隔休眠S秒

四.使用實例:

1.顯示log1文件最後3行內容

命令:

tail -n 3 log1

輸出:

hc@hc-virtual-machine:~/snap$ nl -b a log1
     1  我是log1的第一行
     2  
     3  我是log1的第三行
     4  我是log1的第四行
     5  我是log1的第五行
     6  
     7  我是log1的第七行
hc@hc-virtual-machine:~/snap$ tail -n 3 log1
我是log1的第五行

我是log1的第七行

2. 從第3行開始顯示log1文件內容

命令:

tail -n +3 log1

輸出:

hc@hc-virtual-machine:~/snap$ nl -b a log1
    1  我是log1的第一行
    2  
    3  我是log1的第三行
    4  我是log1的第四行
    5  我是log1的第五行
    6  
    7  我是log1的第七行
hc@hc-virtual-machine:~/snap$ tail -n +3 log1
我是log1的第三行
我是log1的第四行
我是log1的第五行

我是log1的第七行

3.循環刷新查看文件內容

命令:

tail -f test.log

輸出:

hc@hc-virtual-machine:~/snap$ ping 127.0.0.1 > test.log & 
[1] 24615
hc@hc-virtual-machine:~/snap$ tail -f test.log
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.065 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.068 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.157 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.067 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.034 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.043 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.031 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.076 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.067 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.063 ms
^C
hc@hc-virtual-machine:~/snap$ ps -ef | less
[1]+  已殺死               ping 127.0.0.1 > test.log

說明:

ping 127.0.0.1 > test.log & //在後臺ping遠程主機。並輸出文件到test.log;這種做法也使用於一個以上的檔案監視。用Ctrl+c來終止。

由於加了&,所以輸出命令一直在後臺運行,想要殺死它就得找到它的pid,然後kill -9 pid,終止輸出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章