1. 程式人生 > >Linux命令之tail

Linux命令之tail

用途說明

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命令。

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

[[email protected] ~]# tail -n +14 1.txt 
14
15
16
17
18
19
20
[[email protected] ~]#

示例二 動態跟蹤tomcat輸出

動態跟蹤tomcat輸出。

[[email protected] 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 
[[email protected] 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選項了。

[[email protected] 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 
[[email protected] imx_server]#

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

示例四 處理二進位制檔案

[[email protected] ~]# seq 20 >1.txt 
[[email protected] ~]# cat 1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[[email protected] ~]# 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
[[email protected] ~]# tail -c 10 1.txt | hexdump -C 
00000000  0a 31 38 0a 31 39 0a 32  30 0a                    |.18.19.20.|
0000000a
[[email protected] ~]# tail -c -10 1.txt | hexdump -C 
00000000  0a 31 38 0a 31 39 0a 32  30 0a                    |.18.19.20.|
0000000a
[[email protected] ~]# 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
[[email protected] ~]#

相關推薦

移位運算、Arrays中的copyOf、java.util.AbstractCollection、linux命令tail

xca eid width gte 技術分享 blank strac eba tail 移位運算:http://www.cnblogs.com/hongten/p/hongten_java_yiweiyunsuangfu.html。 ---------- Arrays中的c

Linux命令tail

用途說明 tail命令可以輸出檔案的尾部內容,預設情況下它顯示檔案的最後十行。它常用來動態監視檔案的尾部內容的增長情況,比如用來監視日誌檔案的變化。與tail命令對應的是head命令,用來顯示檔案頭部內容。 常用引數 格式:tail file 輸出指定檔案file的尾部內

linux命令head、tail命令具體解釋

-c pri fadein rip lin comment pen inux ont head 語法 樣例 tail 語法 樣例 head和tail組合使用方法舉例 head 語法 head [-n -k ].

Linux基本命令tail

存在 ima 並且 分享圖片 img 更新 ESS dbd 技術 tail:輸出文件的後N行,默認10行格式:-n [number] 參數: -f 常用於查閱正在改變的日誌文件。tail -f filename 會把 filename 文件裏的最尾部的內容顯示在屏幕上,並

Linux命令檢視日誌等實時檔案命令(less 、tail)使用

一、less的使用 1)less  檔名,即可快速開啟檔案 2)相關檢視搜尋 3)利用鍵盤向上向下箭頭鍵盤上的向上和向下箭頭,點選一次向下簡單,檔案內容往下讀取一行;點選一次向上箭頭,檔案內容,往上翻滾一行。 4)page down 和page up利用鍵盤的page down 和page

Linux命令stty

repl 位置 而不是 connector 啟動 previous dsr 根據 pass 用途說明 stty命令用於顯示和修改終端行設置(change and print terminal line settings)。 常用參數 stty命令不帶參數可以打印終端行設置,

Linux命令kill

多種方式 輸入 字符 指定 避免 選項 con 擴展 firefox 精通Linux的kill命令    不管你使用哪種操作系統,你一定會遇到某個行為失常的應用,它把自己鎖死並拒絕關閉。在Linux(還有Mac)。你能夠用一個"kill"命令強制終結它。在這個教程中,我

Linux命令netstat

unknown 網絡相關 mes packet forward 提示 send 狀態 統計 Linux命令之netstat詳解 簡介 Netstat 命令用於顯示各種網絡相關信息,如網絡連接,路由表,接口狀態 (Interface Statistics),masquerad

linux 命令 apt-get

檢查 end 修正 blog font span man 損壞 /var/ apt-get 是一個下載安裝軟件包的簡單命令行接口 使用方法: apt-get [OPTIONS] [COMMANDS] [PACKAGE_NAMES] OPTIONS:

linux 命令 watch

html shell ati watch命令 art track pwd -s 周期 watch能夠幫你監測一個命令的執行結果,省得你一遍遍的手動執行。在Linux下。watch是周期性的執行下個程序。並全屏顯示執行結果。你能夠拿他來監測你想要的一切命令的結果變化,

Linux命令CP詳解

linux命令之cp詳解Linux命令之CP詳解嘿嘿,又一周過去了,大家過的怎麽樣呢,在這一周時間裏,小編可是又學到不少新知識呢。今天呢,小編就和大家分享一下Linux中我們常用的CP的命令,這裏的cp可是copy的簡寫噢。(容我嘚瑟一下) 學過linux的都知道,在我們操作的過程中,我們常常會用到cp這個命

Linux命令——id

命令 linux id id命令是查看用戶相關屬性信息。顯示真實有效的id(UID)和組ID(GID),UID是對應用戶的單一身份標識,GID則是對應多個UID。常用參數:  -g或--group  顯示用戶所屬群組的ID。  -G或--groups  顯示用戶所屬附加群組的ID。  -n或--n

linux 命令top

設置 smd gnome ont str 優先級 ive set tld top命令是顯示當前系統正在執行的進程相關信息。包含進程ID、內存占用率等;top命令格式例如以下: top [OPTIONS] OPTIONS: -b 批處理 -c

Linux命令uptime

and sin bsp 登錄 logs 0.00 mage 查看 工具包 這是什麽 uptime用來查看系統已經啟動了多長時間了。 它顯示的信息和w命令的頭(第一行)是一樣一樣的。 舉個栗子 舉一個實際的應用場景: 比如發現服務器上的某些沒有加入開機啟動的服務掛了一

Linux命令命令查找方式

命令查找順序1 命令類型 linux中命令類型分為兩類,一類為shell中自帶的命令,即內部命令,而其它的在文件系統之上的命令程序,即外部命令. 內部命令的優點: 命令程序在內存中,使用更快捷 相關命令: type,enable type -

測試過程中常用的linux命令【查看文件指定行的內容】

sed head tail 在開展測試工作的過程中,通常要接觸到服務器,對於linux服務器,總結一些常用的命令。 準備工作為了能直觀展示命令結果,使用腳本創建一個文件,在顯示文件內容的同時,也直觀的顯示行號。#!/bin/bash FileName=TestFile.log touch ./$

測試過程中常用的linux命令【查找指定的文件內容】

vi grep 之前遇到過這種情況,在查看日誌定位問題時,會將log 傳輸到本地,然後用本地的文本編輯工具打開文件,然後查找關鍵字,再進行分析。 這樣做無疑會降低效率。整理了幾個常用的命令,可以進行快速的查找。測試文件:文件名稱為execution.log, 文件內容如下:[error] Timed o

Linux命令bc

命令 logs oca cal clas 交互 浮點 linux blog bc命令是一種支持任意精度的交互執行的計算器語言。 bash內置了對整數四則運算的支持,但是並不支持浮點運算,而bc命令可以很方便的進行浮點運算,當然整數運算也不再話下。 如果沒有可以下載

Linux命令pwd和cd

linux 基本操作命令 1. pwd print working directory 打印當前所在路徑 2.cd change directory 改變路徑、切換路徑 cd /home ( 沒有選項,有參數) cd /home/book 為了簡化輸入: cd / 切換到根目錄 cd

Linux命令mkdir和rmdir

linux mkdir rmdir mkdir make directory 創建目錄 1)mkdir 目錄名 舉例: mkdir dir0 2) mkdir -p 父目錄/子目錄 mkdir -p dir1/dir2 其中,-p是parents的縮寫 連續創建多級目錄,比如父目錄和子目錄,如果父