CentOS裡tail命令詳解整理

來源:互聯網
上載者:User

tail

名稱:tail - output the last part of files 輸出檔案最後的那部分內容

功能描述:tail命令從指定點開始將檔案寫到標準輸出,使用-f選項可以方便的查閱正在改變的記錄檔,tail –f filename會到filename尾部的內容輸出並且不斷的重新整理,可以看到不斷更新的內容。

參數:

-f 迴圈讀取 follow

-v 顯示詳細的處理資訊

-n 顯示行數

-p --pid 與-f合用,表示當-f所指定的檔案,在所產生它的程式死掉後,結束。通過pid來識別。

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

-q –quiet 從不輸出頭給檔案。

執行個體:


1)顯示檔案末尾內容

[root@bogon ~]# tail -n 10 /var/log/messages

Apr 25 22:25:16 bogon last message repeated 14times

Apr 25 22:26:21 bogon last message repeated 29times

Apr 25 22:30:59 bogon last message repeated 2 times

Apr 25 22:31:20 bogon last message repeated 12times

Apr 25 22:31:22 bogon dhclient: DHCPREQUEST on eth0to


2)迴圈查看檔案內容,不斷更新,當pid死掉時查看結束。

[root@bogon ~]# ping 192.168.40.1 > test.log&

[1] 4911

[root@bogon ~]# tail -n 3 -f test.log

64 bytes from 192.168.40.1: icmp_seq=11 ttl=255time=0.859 ms

64 bytes from 192.168.40.1: icmp_seq=12 ttl=255time=0.767 ms

64 bytes from 192.168.40.1: icmp_seq=13 ttl=255time=0.564 ms

64 bytes from 192.168.40.1: icmp_seq=14 ttl=255time=0.748 ms

64 bytes from 192.168.40.1: icmp_seq=15 ttl=255time=0.724 ms

[root@bogon ~]# jobs

[1]+ Running ping192.168.40.1 > test.log &

[root@bogon ~]# fg 1

ping 192.168.40.1 > test.log

說明:顯示把ping 192.168.40.1 >test.log置入到後台執行。當我們查看完成之後,需要通過命令jobs 查看後台工作 fg 1 把它調回到前台來,然後通過ctrl+c 來結束。

另:有一點就是ping 192.168.40.1 > test.log 那麼test.log的內容不是應該就只有一條,因為後來的覆蓋掉前面的。但我都結束之後cat查看test.log的內容卻是很多?


3)排除前內容最前面的5行內容,從第5行開始顯示。

[root@bogon ~]# tail -n +5 2.log

5

6

7

8

9

-----------------------後續自我小結--------------------

tail主要就是用來查看我們的檔案,從文章後面開始查看。主要的一個功能就是-f follow持續的查看

檔案的更新內容。主要的還是體現在能夠查看內容。當然能夠查看到的內容都是可以被處理的:比如進行選擇,awk截取顯示,定向到另一個檔案等等..這個就是靈活的地方。這個就要和我們的處理命令結合在一起了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.