本文連接:http://codingstandards.iteye.com/blog/832575 (轉載請註明連接)java
tail命令能夠輸出文件的尾部內容,默認狀況下它顯示文件的最後十行。它經常使用來動態監視文件的尾部內容的增加狀況,好比用來監視日誌文件的變化。與tail命令對應的是head命令,用來顯示文件頭部內容。web
格式:tail fileapache
輸出指定文件file的尾部內容,默認輸出最後十行內容(output the last part of files。Print the last 10 lines of each FILE to standard output. )tomcat
格式:tail file1 file2 ...bash
指定多個文件時,會顯示每一個文件的文件名稱,再顯示該文件的尾部內容(With more than one FILE, precede each with a header giving the file name.)網絡
格式:tailapp
格式:tail -ui
不指定文件時,代表從標準輸入讀取內容,這一般用在管道線後面,把前一個命令的輸出做爲tail的輸入內容(With no FILE, or when FILE is -, read standard input.)spa
格式:tail -n file.net
格式: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輸出。
[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]#
通過個人試驗,發現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 ~]#