歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux系統中tail命令的使用詳解

linux系統中tail命令的使用詳解

日期:2017/8/19 9:45:26   编辑:Linux技術

  Linux系統中tail命令是查看文件末尾內容的主要功能,下面由學習啦小編為大家整理了linux系統中tail命令的使用詳解,希望對大家有幫助!

  linux系統中tail命令的使用詳解

  1 命令功能

  tail命令用於顯示文件中末尾的內容(默認顯示最後10行內容)

  2 命令語法

  tail 【選項參數】 【文件名1】 【文件名2】

  linux系統中tail命令參數

  -f 用於循環讀取文件的內容,監視文件的增長

  -F 與-f類似,區別在於當將監視的文件刪除重建後-F仍能監視該文件內容-f則不行,-F有重試的功能,會不斷重試

  -c N 顯示文件末尾N字節的內容

  -n 顯示文件末尾n行內容

  -q 顯示多文件的末尾內容時,不顯示文件名

  -v 顯示多文件的末尾內容時,顯示文件名(此為tail的默認選項)

  -s N 與-f合用,表示休眠N秒後在讀取文件內容(默認為1s)

  --pid=<進程號PID> 與“-f”選項連用,當指定的進程號的進程終止後,自動退出tail命令

  linux系統中tail命令實例

  實例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</p> <p>[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

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

  補充:linux系統中查看文件內容的其它命令

  最基本的是cat、more和less。

  1. 如果你只想看文件的前5行,可以使用head命令,如:

  head -5 /etc/passwd

  2. 如果你想查看文件的後10行,可以使用tail命令,如:

  tail -10 /etc/passwd 或 tail -n 10 /etc/passwd

  tail -f /var/log/messages

  參數-f使tail不停地去讀最新的內容,這樣有實時監視的效果 用Ctrl+c來終止!

  3. 查看文件中間一段,你可以使用sed命令,如:

  sed -n '5,10p' /etc/passwd

  這樣你就可以只查看文件的第5行到第10行。

Copyright © Linux教程網 All Rights Reserved