Linux命令之tail - 輸出文件尾部/動態監視文件尾部


本文鏈接:http://codingstandards.iteye.com/blog/832575   (轉載請注明鏈接)

用途說明

tail命令可以輸出文件的尾部內容,默認情況下它顯示文件的最后十行。它常用來動態監視文件的尾部內容的增長情況,比如用來監視日志文件的變化。與tail命令對應的是head命令,用來顯示文件頭部內容。

 

常用參數

格式:tail file

輸出指定文件file的尾部內容,默認輸出最后十行內容(output the last part of files。Print  the  last 10 lines of each FILE to standard output. )

 

格式:tail file1 file2 ...

指定多個文件時,會顯示每個文件的文件名稱,再顯示該文件的尾部內容(With more than one FILE, precede each with a header giving the file name.)

 

格式:tail

格式:tail -

不指定文件時,表明從標准輸入讀取內容,這通常用在管道線后面,把前一個命令的輸出作為tail的輸入內容(With no FILE, or when FILE is -, read standard input.)

 

格式:tail -n file

格式:tail -n n file

格式:tail --lines=n

顯示文件最后n 行,比如tail -20 file就是顯示文件最后10行,這個參數可以配合其他參數與使用。注意上面三種格式的斜體n 是實際要顯示的行數的數值。

 

注意:tail -n可以顯示最后n行的文本內容。那么有沒有一種方式顯示從n行開始的文本內容,答案是肯定的。

tail -n +4 file表示顯示文件file從第4行開始的內容。從1開始計數。

 

格式:tail -f file

動態跟蹤文件file的增長情況(output appended data as the file grows),tail會每隔一秒去檢查一下文件是否增加新的內容,如果增加就追加在原來的輸出后面顯示。但這種情況,必須保證在執行tail命令時,文件已經存在。

如果想終止tail -f的輸出,按Ctrl+C中斷tail程序即可。如果按Ctrl+C不能中斷輸出,那么可以在別的終端上執行killall tail強行終止。

 

注意:采用tail -f來監控文件變化情況時,在某些情況會不太靈。比如在java應用程序中采用log4j日志時,每隔1個小時生成一個新的日志文件,當前的日志輸出在LOG4J.LOG中,當一個小時過去后,log4j會將LOG4J.LOG改名成LOG4J.yyyy-mm-dd-HH的形式。那么這個時候tail -f就不能動態輸出新的日志內容了。tail命令本身提供了很多參數,似乎都不能完美的解決這個問題。最后只好編寫了一個腳本ftail.sh來跟蹤日志,詳見《Linux下實時跟蹤log4j日志文件的bash腳本 - 增強了tail -f的功能 》。剛才我仔細查看了tail的手冊頁,發現tail -F就能夠做到跟蹤這種類型的日志。轉念一想,這種需求應該早就被Linux世界的人給滿足了的。

 

格式:tail -F file

格式:tail --follow=name --retry file

功能與tail -f file相同,也是動態跟蹤文件的變化,不同的是執行此命令時文件可以不存在。

 

以上處理都是針對文本文件的,下面是針對二進制文件的情形。

 

格式:tail -c n file

取文件file的最后n個字節。

 

格式:tail -c +n file

取文件file的第n個字節后的內容。從1開始計數。

使用示例

示例一 輸出文件尾部

先使用seq命令輸出20個數字保存到1.txt,然后嘗試使用tail命令。

[root@new55 ~]# seq 20 >1.txt 
[root@new55 ~]# cat 1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@new55 ~]# tail 1.txt 
11
12
13
14
15
16
17
18
19
20
[root@new55 ~]# tail -3 1.txt 
18
19
20
[root@new55 ~]# tail -n 3 1.txt 
18
19
20
[root@new55 ~]# tail --lines=3 1.txt 
18
19
20

[root@new55 ~]# tail -n +14 1.txt 
14
15
16
17
18
19
20
[root@new55 ~]#

 

示例二 動態跟蹤tomcat輸出

動態跟蹤tomcat輸出。

[root@web logs]# tail -f catalina.out 
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:619)
2010-12-03 13:23:02,236 [http-80-15] DEBUG mhr.roi.MhrManager - MhrGetJobReq={seq=991,job_id='86130469-0006'}
2010-12-03 13:23:02,301 [http-80-15] DEBUG mhr.roi.MhrManager - MhrGetJobRsp={seq=991,result=0(成功),,info={job_id='86130469-0006',employer_id=86130469,employer_name=無錫富士時裝有限公司,,,job_title='|570309|',job_title0='文員',job_type=f(全職),issue_time='2010-11-03 00:00:00.0',work_address='1902',work_address0=無錫市,desired_count='1',,,,,,job_desc=大專,1年以上5s管理工作經驗,電腦操作熟練。,required_experience=1(一年以上),,,,required_degree=15(大專),,,,,,,valid_days=30,access_count=12,expire_time='2010-12-03 00:00:00.0',job_status=1(過期),,,,,,contact_name=王小姐,contact_number=0510-85550088,remarks='★此職位是從后台導入的
',enter_time='2010-11-03 09:45:11.0',change_time=2010-12-03 02:18:05.0,,job_seq=123201,,required_min_age=22,,,accommodations=面議,serve_principal=wjw12580,job_summary=大專,1年以上5s管理工作經驗,電腦操作熟練,,}}
2010-12-03 13:23:02,302 [http-80-15] DEBUG mhr.roi.MhrManager - MhrGetEmployerReq={seq=0,employer_id='86130469'}
2010-12-03 13:23:02,304 [http-80-15] DEBUG mhr.roi.MhrManager - MhrGetEmployerRsp={seq=0,result=0(成功),,info={employer_id='86130469',employer_name=無錫富士時裝有限公司,employer_region=1902,employer_address=無錫市濱湖鎮山水城科技園8號,,employer_desc=無錫富士時裝(集團)有限公司成立於1992年,是中日韓合資企業。主要生產高檔針織時裝,產品全部外銷,連續多年榮獲“中國行業500強”、“全國工業重點行業效益十佳企業”、“無錫市百佳企業”等稱號。公司總部位於江蘇省無錫市濱湖鎮山水城科技園,全新的廠房設施,占地面積30畝。公司分別在蘇州、泰興、鹽城、徐州設有分廠,集團公司現有職工1500多人,年銷售額近3億元。,,,,,,open_mode=5(系統自動操作),open_time='2010-11-03 09:45:10.0',,,,,contact_name=王小姐,contact_number=0510-85550088,,,,,employer_status=1(已經開通),,,login_password=871386,,agency=false,balance=100.0000,,,,,serve_principal=wjw12580,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,remarks='此帳號由“12580zgz-鹽城維克多網絡傳媒有限公司”導入',enter_time='2010-11-03 09:45:10.0',}}
Ctrl+C 
[root@web logs]#

 

示例三 動態跟蹤log4j日志

經過我的試驗,發現tail -F功能的強大,它等同於--follow=name --retry。如果你跟蹤的文件被移動或者改名后, 你還想繼續tail它, 你可以使用這個選項。

tail手冊頁中關於--retry的說明:keep trying to open a file even if it is inaccessible when tail starts or  if  it  becomes  inaccessible later; useful when following by name, i.e., with --follow=name。 tail命令開始執行時文件不存在或者執行過程中文件不能訪問,會不斷重試。

關於--follow的說明:-f, --follow[={name|descriptor}] output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent --follow=descriptor表明跟蹤的是文件描述符,  --follow=name表明跟蹤的是文件名稱。 如果文件名稱改掉之后,還想繼續跟蹤原文件名稱對應的尾部內容,就得使用-F選項而不是-f選項了。

[root@web imx_server]# tail -F log/IMX.LOG 
14:13:28.892  INFO ImxConnection[6] imx.server.ImxConnection - RX IMX_ACTIVE_TEST{seq=3460,client_id=1291343201649042,presence_status=1(presence_status_online),}
14:13:28.892 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006417 (01/02/00) - Connection #9 served
14:13:28.892  INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow - EXEC SQL UPDATE imx_online_info SET last_active_time = '2010-12-03 14:13:28.0' WHERE account = 'zhy'
14:13:28.894 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - UPDATE imx_online_info SET last_active_time = '2010-12-03 14:13:28.0' WHERE account = 'zhy';  (1 milliseconds)
14:13:28.894 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006417 (00/02/00) - Connection #9 returned (now AVAILABLE)
14:13:29.625  INFO ImxConnection[6] imx.server.ImxConnection - RX IMX_ACTIVE_TEST{seq=3461,client_id=1291343201649042,presence_status=1(presence_status_online),}
14:13:29.626 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006418 (01/02/00) - Connection #8 served
14:13:29.626  INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow - EXEC SQL UPDATE imx_online_info SET last_active_time = '2010-12-03 14:13:29.0' WHERE account = 'zhy'
14:13:29.627 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - UPDATE imx_online_info SET last_active_time = '2010-12-03 14:13:29.0' WHERE account = 'zhy';  (0 milliseconds)
14:13:29.653 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006418 (00/02/00) - Connection #8 returned (now AVAILABLE)
Ctrl+C 
[root@web imx_server]#

 

總結一下:要想跟蹤會更名的日志的話,用tail -F而不是tail -f。

 

示例四 處理二進制文件

[root@new55 ~]# seq 20 >1.txt 
[root@new55 ~]# cat 1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@new55 ~]# hexdump -C 1.txt 
00000000  31 0a 32 0a 33 0a 34 0a  35 0a 36 0a 37 0a 38 0a  |1.2.3.4.5.6.7.8.|
00000010  39 0a 31 30 0a 31 31 0a  31 32 0a 31 33 0a 31 34  |9.10.11.12.13.14|
00000020  0a 31 35 0a 31 36 0a 31  37 0a 31 38 0a 31 39 0a  |.15.16.17.18.19.|
00000030  32 30 0a                                          |20.|
00000033
[root@new55 ~]# tail -c 10 1.txt | hexdump -C 
00000000  0a 31 38 0a 31 39 0a 32  30 0a                    |.18.19.20.|
0000000a
[root@new55 ~]# tail -c -10 1.txt | hexdump -C 
00000000  0a 31 38 0a 31 39 0a 32  30 0a                    |.18.19.20.|
0000000a
[root@new55 ~]# tail -c +10 1.txt | hexdump -C 
00000000  0a 36 0a 37 0a 38 0a 39  0a 31 30 0a 31 31 0a 31  |.6.7.8.9.10.11.1|
00000010  32 0a 31 33 0a 31 34 0a  31 35 0a 31 36 0a 31 37  |2.13.14.15.16.17|
00000020  0a 31 38 0a 31 39 0a 32  30 0a                    |.18.19.20.|
0000002a
[root@new55 ~]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM