1、實(shí)時(shí)輸出日志
tail -f filename
tail -f filename | grep keyword //如果keyword中有空格吆鹤,需要加雙引號(hào)
tail -f filename | grep -n keyword //實(shí)時(shí)輸出keyword前后n行的日志內(nèi)容
2、查看歷史日志
2.1 cat
cat filename
cat -n filename //與 nl filename 等價(jià)
cat filename | grep keyword
cat -n filename | grep keyword //顯示行數(shù)
cat -n filename | grep -10 keyword //查看keyword前后10行的內(nèi)容沾凄,并顯示行數(shù)
cat filename1 > filename2 //創(chuàng)建filename2知允,并將filename1中的內(nèi)容復(fù)制到filename2
2.2 tac
Mandatory arguments to long options are mandatory for short options too.
-b, --before attach the separator before instead of after
-r, --regex interpret the separator as a regular expression
-s, --separator=STRING use STRING as the separator instead of newline
--help display this help and exit
--version output version information and exit
2.3 tail
tail filename //默認(rèn)查看最后10行
tail -n (-)20 filename //查看最后20行的內(nèi)容(帶不帶負(fù)號(hào)都一樣)
tail -n +20 filename //查看第20行至最后的內(nèi)容
2.4 head
與tail相反,tail查看文件后n行內(nèi)容温鸽,head查看前n行。n前面的負(fù)號(hào)表示倒數(shù)姑尺,正號(hào)表示正數(shù)蝠猬。
head filename //默認(rèn)查看前10行
head -n -20 filename //從第1行至第倒數(shù)20行
head -n (+)20 filename //查看前20行
2.5 不解壓查看壓縮日志文件
zcat compressedFilename
zcat compressedFilename | grep -5 keyword
zgrep keyword compressedFilename //與zcat compressedFilename | grep keyword等價(jià)
// 如果文件經(jīng)過tar與gzip雙層壓縮,需要加上--binary-files參數(shù)
cat compressedFilename | grep --binary-files=text"keyword"
3榆芦、命令組合
cat -n filename | tail -n +101 | head -n 20 //顯示行號(hào),并打印第101行至第120行的內(nèi)容
cat -n filename | tail -n +101 | head -n 20 | grep keyword
...