每天一個linux命令14-tail

1. 命令解析

命令用途

把文件的末尾N行顯示到標準輸出,默認顯示10行,還可通過 tail -f file 來持續(xù)輸出文件的數(shù)據(jù)。

命令格式

tail [OPTION]... [FILE]...

命令參數(shù)

-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --follow=name --retry
-n, --lines=K output the last K lines, instead of the last 10;
or use -n +K to output starting with the Kth
--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to see if it has been unlinked or renamed
(this is the usual case of rotated log files);
with inotify, this option is rarely useful
--pid=PID with -f, terminate after process ID, PID dies
-q, --quiet, --silent never output headers giving file names
--retry keep trying to open a file if it is inaccessible
-s, --sleep-interval=N with -f, sleep for approximately N seconds
(default 1.0) between iterations;
with inotify and --pid=P, check process P at
least once every N seconds

2. 示例

2.1 顯示文件末尾10行

[root@test headTest]# tail hisotry.log 
  213  less history.log 
  214  cd /root/test
  215  ls
  216  mkdir headTest
  217  ls
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.2 顯示文件末尾5行 -n

[root@test headTest]# tail -5 hisotry.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.3 實時跟蹤文件(默認1秒刷新一次) -f

[root@test headTest]# ping localhost > ping.log&
[1] 4971
[root@test headTest]# tail -f ping.log
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.011 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.051 ms

通過CTRL+C結(jié)束實時監(jiān)控

2.4 定時實時跟蹤文件 -fs

[root@test headTest]# ping localhost > ping.log&
[2] 5116
[root@test headTest]# tail -fs2 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=10 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=220 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=221 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=222 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=223 ttl=64 time=0.081 ms

每隔2秒刷新,顯示文件最新內(nèi)容

2.5 從第5行開始顯示剩余所有內(nèi)容 -n +N

[root@test headTest]# tail -n +5 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.081 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.050 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.029 ms
...省略...

2.6 顯示多個文件

root@test headTest]# tail -n 5 hisotry.log ping.log 
==> hisotry.log <==
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

==> ping.log <==
64 bytes from localhost (127.0.0.1): icmp_seq=331 ttl=64 time=0.062 ms
64 bytes from localhost (127.0.0.1): icmp_seq=332 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=333 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=334 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=335 ttl=64 time=0.025 ms

2.7 不顯示文件名 -q

[root@test headTest]# tail -qn 5 hisotry.log ping.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log
64 bytes from localhost (127.0.0.1): icmp_seq=373 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=374 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=375 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=376 ttl=64 time=0.032 ms
64 bytes from localhost (127.0.0.1): icmp_seq=377 ttl=64 time=0.030 ms
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 12,228評論 2 33
  • 我是一個喜歡在家吃飯的人,一般不太下館子。可是什么吃多了都會膩,下館子就作為調(diào)劑,偶爾沾些大油葷腥,放縱一下腸胃,...
    Betty0614203閱讀 260評論 0 0
  • 夜。 無星光閃爍, 只見小飛蟲在路燈下晃動。 無微風(fēng)徐來, 只聽老蟬不停歇的嘶喊。 夜, 枝頭一抹絢爛, 悄悄吐露...
    冬至而已閱讀 126評論 1 1
  • 《孩子,把你的手給我》D2學(xué)而思20170112 “語言暴力像一把消音槍, 謀殺了孩子的尊嚴和人生, 除了當時的痛...
    遙遠曉然閱讀 796評論 0 0