tail 命令可用於查看文件的內容。
最常用到的是參數 -f ,用於查閱正在改變的日志文件。
命令格式:
tail [參數] [文件]
參數:
- -f 循環讀取,監視文件的尾部內容(默認10行,相當於增加參數 -n 10)
- -q 不顯示處理信息
- -v 顯示詳細的處理信息
- -c<數目> 顯示的字節數
- -n<行數> 顯示文件的尾部 n 行內容
- --pid=PID 與-f合用,表示在進程ID,PID死掉之后結束
- -q, --quiet, --silent 從不輸出給出文件名的首部
- -s, --sleep-interval=S 與-f合用,表示在每次反復的間隔休眠S秒
可以執行命令tail --help
就能看到tail
命令介紹
-c, --bytes=K output the last K bytes;
or use -c +K to output bytes starting with the Kth of each file
輸出最后的 K 個字節;
或者使用 -c +K 從每個文件的第K字節開始打印。
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
隨着文件的增長,輸出附加數據;(動態輸出最新的信息);
沒有選項參數意味着“描述符”
-F same as --follow=name --retry
與 --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
輸出最后的K行,而不是最后的10行;
或者使用-n +K從第K個開始輸出
--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
使用——follow=name,在N次(默認為5次)迭代后,重新打開一個大小沒有改變的文件,看看它是否被解除鏈接或重命名(這是旋轉日志文件的常見情況);
對於inotify,這個選項很少有用
--pid=PID with -f, terminate after process ID, PID dies
與“-f”選項連用,當指定的進程號的進程終止后,自動退出tail命令
-q, --quiet, --silent never output headers giving file names
當有多個文件參數時,不輸出各個文件名
--retry keep trying to open a file if it is inaccessible
即是在tail命令啟動時,文件不可訪問或者文件稍后變得不可訪問,都始終嘗試打開文件。使用此選項時需要與選項“——follow=name”連用
-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
與“-f”選項連用,指定監視文件變化時間隔的秒數(默認為1.0);
使用inotify和-pid=P,每N秒檢查進程P至少一次
-v, --verbose always output headers giving file names
當有多個文件參數時,總是輸出各個文件名
--help display this help and exit
顯示此幫助信息並退出
--version output version information and exit
顯示版本信息並退出
其中 tail -f與tail -F的區別:
tail -f
等同於–follow=descriptor,根據文件描述符進行追蹤,當文件改名或被刪除,追蹤停止
tail -F
等同於–follow=name --retry,根據文件名進行追蹤,並保持重試,即該文件被刪除或改名后,如果再次創建相同的文件名,會繼續追蹤
tailf
等同於tail -f -n 10(貌似tail -f或-F默認也是打印最后10行,然后追蹤文件),與tail -f不同的是,如果文件不增長,它不會去訪問磁盤文件,減少了磁盤訪問,所以tailf特別適合那些便攜機上跟蹤日志文件
如果遇到日志文件較大的時候,通常會輸出的行數來查看日志:
// 默認顯示 log 文件的最后 10 行
tail test.log
// 顯示 log 文件的最后 10 行,同時跟蹤名為文件的增長情況,直到您按下(Ctrl-C)組合鍵停止顯示。
tail -f test.log
// 顯示 log 文件的最后 n 行,同時跟蹤名為文件的增長情況,直到您按下(Ctrl-C)組合鍵停止顯示。
tail -nf test.log
// 顯示文件的最后 10 行
tail -n 10 filename
// 文件的第 9 行不顯示,顯示第 10 行到末尾行
tail -n -10 filename
// 顯示文件的第 10 行到末尾行
tail -n +10 filename
逆序顯示filename最后10行。
tail -r -n 10 filename
// 顯示第20行至末尾
tail +20 test.log
// 顯示最后10個字符
tail -c 10 test.log
退出:
按下CTRL+C。