學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux系統(tǒng)中tail命令的使用詳解

linux系統(tǒng)中tail命令的使用詳解

時間: 佳洲1085 分享

linux系統(tǒng)中tail命令的使用詳解

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

  linux系統(tǒng)中tail命令的使用詳解

  1 命令功能

  tail命令用于顯示文件中末尾的內(nèi)容(默認顯示最后10行內(nèi)容)

  2 命令語法

  tail 【選項參數(shù)】 【文件名1】 【文件名2】

  linux系統(tǒng)中tail命令參數(shù)

  -f 用于循環(huán)讀取文件的內(nèi)容,監(jiān)視文件的增長

  -F 與-f類似,區(qū)別在于當將監(jiān)視的文件刪除重建后-F仍能監(jiān)視該文件內(nèi)容-f則不行,-F有重試的功能,會不斷重試

  -c N 顯示文件末尾N字節(jié)的內(nèi)容

  -n 顯示文件末尾n行內(nèi)容

  -q 顯示多文件的末尾內(nèi)容時,不顯示文件名

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

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

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

  linux系統(tǒng)中tail命令實例

  實例1:顯示文件末尾內(nèi)容

  命令:

  tail -n 5 log2014.log

  輸出:

  復(fù)制代碼代碼如下:

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

  2014-09

  2014-10

  2014-11

  2014-12

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

  說明:

  顯示文件最后5行內(nèi)容

  實例2:循環(huán)查看文件內(nèi)容

  命令:

  tail -f test.log

  輸出:

  復(fù)制代碼代碼如下:

  [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;這種做法也使用于一個以上的檔案監(jiān)視。用Ctrl+c來終止。

  實例3:從第5行開始顯示文件

  命令:

  tail -n +5 log2014.log

  輸出:

  復(fù)制代碼代碼如下:

  [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系統(tǒng)中查看文件內(nèi)容的其它命令

  最基本的是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

  參數(shù)-f使tail不停地去讀最新的內(nèi)容,這樣有實時監(jiān)視的效果 用Ctrl+c來終止!

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

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

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

3645368