歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux命令每天必學(15)之tail 命令

linux命令每天必學(15)之tail 命令

日期:2017/3/1 17:31:43   编辑:Linux技術

linux tail命令用途是依照要求將指定的文件的最後部分輸出到標准設備,通常是終端,通俗講來,就是把某個檔案文件的最後幾行顯示到終端上,假設該檔案有更新,tail會自己主動刷新,確保你看到最新的檔案內容。
tail命令語法

復制代碼代碼如下:
tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

參數解釋:
-f 該參數用於監視File文件增長。
-c Number 從 Number 字節位置讀取指定文件
-n Number 從 Number 行位置讀取指定文件。
-m Number 從 Number 多字節字符位置讀取指定文件,比方你的文件假設包括中文字,假設指定-c參數,可能導致截斷,但使用-m則會避免該問題。
-b Number 從 Number 表示的512字節塊位置讀取指定文件。
-k Number 從 Number 表示的1KB塊位置讀取指定文件。
File 指定操作的目標文件名稱
上述命令中,都涉及到number,假設不指定,默認顯示10行。Number前面可使用正負號,表示該偏移從頂部還是從尾部開始計算。
tail可運行文件一般在/usr/bin/以下。
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
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
==============================

Copyright © Linux教程網 All Rights Reserved