日志一般是記載每天所做的工作。在計算機科學中,日志是指服務器等電腦設備或軟件的運作記錄(Server log)。在電腦設備和軟件出現問題時,日志是我們在排查問題的一個重要依據。查詢日志是用戶記錄從客戶端收到所有數據庫或操作系統的查詢,同時也包括了每一個客戶端的鏈接和斷開鏈接。
如今各種花式日志系統大行其道,但在此同時也不要忘記linux查看日志的基礎功能,今天就講講linux查看日志的常用基礎功能
? 1 less
1.1 less -N 日志文件名.log
less -N test.log然后輸入"/context"搜索context關鍵字
點擊鍵盤↑ ↓可以滾動,點擊 N 可以查看上一個,n可以查看下一個
1.2 less詳解
SUMMARY OF LESS COMMANDS
Commands marked with * may be preceded by a number, N.
Notes in parentheses indicate the behavior if N is given.
A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.
h H Display this help.
q :q Q :Q ZZ Exit.
---------------------------------------------------------------------------
MOVING
e ^E j ^N CR * Forward one line (or N lines).
y ^Y k ^K ^P * Backward one line (or N lines).
f ^F ^V SPACE * Forward one window (or N lines).
b ^B ESC-v * Backward one window (or N lines).
z * Forward one window (and set window to N).
w * Backward one window (and set window to N).
ESC-SPACE * Forward one window, but don't stop at end-of-file.
d ^D * Forward one half-window (and set half-window to N).
u ^U * Backward one half-window (and set half-window to N).
ESC-) RightArrow * Left one half screen width (or N positions).
ESC-( LeftArrow * Right one half screen width (or N positions).
F Forward forever; like "tail -f".
r ^R ^L Repaint screen.
R Repaint screen, discarding buffered input.
---------------------------------------------------
Default "window" is the screen height.
Default "half-window" is half of the screen height.
---------------------------------------------------------------------------
SEARCHING
/pattern * Search forward for (N-th) matching line.
?pattern * Search backward for (N-th) matching line.
n * Repeat previous search (for N-th occurrence).
N * Repeat previous search in reverse direction.
ESC-n * Repeat previous search, spanning files.
ESC-N * Repeat previous search, reverse dir. & spanning files.
ESC-u Undo (toggle) search highlighting.
&pattern * Display only matching lines
---------------------------------------------------
A search pattern may be preceded by one or more of:
^N or ! Search for NON-matching lines.
^E or * Search multiple files (pass thru END OF FILE).
^F or @ Start search at FIRST file (for /) or last file (for ?).
^K Highlight matches, but don't move (KEEP position).
^R Don't use REGULAR EXPRESSIONS.
---------------------------------------------------------------------------
HELP -- Press RETURN for more, or q when done
? 2 grep
2.1 統計文件或者文本中包含匹配字符串的行數 -c 選項
grep -c "context" daily.log
2.2 使用正則表達式 -E 選項
grep -E "[1-9]+" daily.log
搜索以數字2開頭的行:
2.3 使用正則表達式 -E 選項
grep -l "text" file1 file2 file3...
2.4 grep詳解
用法: grep [選項]... PATTERN [FILE]...
在每個 FILE 或是標準輸入中查找 PATTERN。
默認的 PATTERN 是一個基本正則表達式(縮寫為 BRE)。
例如: grep -i 'hello world' menu.h main.c
正則表達式選擇與解釋:
-E, --extended-regexp PATTERN 是一個可擴展的正則表達式(縮寫為 ERE)
-F, --fixed-strings PATTERN 是一組由斷行符分隔的定長字符串。
-G, --basic-regexp PATTERN 是一個基本正則表達式(縮寫為 BRE)
-P, --perl-regexp PATTERN 是一個 Perl 正則表達式
-e, --regexp=PATTERN 用 PATTERN 來進行匹配操作
-f, --file=FILE 從 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小寫
-w, --word-regexp 強制 PATTERN 僅完全匹配字詞
-x, --line-regexp 強制 PATTERN 僅完全匹配一行
-z, --null-data 一個 0 字節的數據行,但不是空行
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
輸出控制:
-m, --max-count=NUM NUM 次匹配后停止
-b, --byte-offset 輸出的同時打印字節偏移
-n, --line-number 輸出的同時打印行號
--line-buffered 每行輸出清空
-H, --with-filename 為每一匹配項打印文件名
-h, --no-filename 輸出時不顯示文件名前綴
--label=LABEL 將LABEL 作為標準輸入文件名前綴
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive
likewise, but follow all symlinks
--include=FILE_PATTERN
search only files that match FILE_PATTERN
--exclude=FILE_PATTERN
skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
文件控制:
-B, --before-context=NUM 打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本結尾的NUM 行
-C, --context=NUM 打印輸出文本NUM 行
-NUM same as --context=NUM
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)
‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
若FILE 為 -,將讀取標準輸入。不帶FILE,讀取當前目錄,除非命令行中指定了-r 選項。
如果少于兩個FILE 參數,就要默認使用-h 參數。
如果有任意行被匹配,那退出狀態為 0,否則為 1;
如果有錯誤產生,且未指定 -q 參數,那退出狀態為 2。
請將錯誤報告給: bug-grep@gnu.org
GNU Grep 主頁: <http://www.gnu.org/software/grep/>
GNU 軟件的通用幫助: <http://www.gnu.org/gethelp/>
根據 關鍵詞 查看日志 并返回關鍵詞所在行:
grep -i "test" ./test.log 返回test.log中包含test的所有行(-i忽略大小寫)
? 3 cat
3.1 查看日志前n行
cat test.log | head -n 5
test.log為文件名,5為行數。
3.2 查看日志尾n行
cat test.log | tail -n 5
3.3 根據關鍵詞查看日志
cat daily.log | grep "context"
3.4 cat詳解
用法:cat [選項]... [文件]...
將[文件]或標準輸入組合輸出到標準輸出。
-A, --show-all 等于-vET
-b, --number-nonblank 對非空輸出行編號
-e 等于-vE
-E, --show-ends 在每行結束處顯示"$"
-n, --number 對輸出的所有行編號
-s, --squeeze-blank 不輸出多行空行
-t 與-vT 等價
-T, --show-tabs 將跳格字符顯示為^I
-u (被忽略)
-v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
如果沒有指定文件,或者文件為"-",則從標準輸入讀取。
示例:
cat f - g 先輸出f 的內容,然后輸出標準輸入的內容,最后輸出g 的內容。
cat 將標準輸入的內容復制到標準輸出。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
請向<http://translationproject.org/team/zh_CN.html> 報告cat 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'cat invocation'
[toutou@localhost front]$
? 4 tail和head命令
4.1 tail常見用法
tail -f test.log 可以動態的查看服務器運行狀態的日志
head -n 5 test.log 顯示top 5行
tail -n 5 test.log 顯示last 5行
tail -n +5 test.log 從第5行開始顯示,顯示第5行以后的
4.2 tail詳解
用法:tail [選項]... [文件]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --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
--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
--pid=PID with -f, terminate after process ID, PID dies
-q, --quiet, --silent never output headers giving file names
--retry keep trying to open a file if it is inaccessible
-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
-v, --verbose always output headers giving file names
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file. K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
如果您希望即時追查一個文件的有效名稱而非描述內容(例如循環日志),默認
的程序動作并不如您所愿。在這種場合可以使用--follow=name 選項,它會使
tail 定期追蹤打開給定名稱的文件,以確認它是否被刪除或被其它某些程序重新創建過。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
請向<http://translationproject.org/team/zh_CN.html> 報告tail 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'tail invocation'
? 5 sed
5.1 查看文件的第5行到第15行
sed -n '5,15p' daily.log
5.2 sed詳解
sed不與初始化文件打交道,它操作的只是一個拷貝,然后所有的改動如果沒有重定向到一個文件,將輸出到屏幕。
sed是一種很重要的文本過濾工具,使用一行命令或者使用管道與grep與awk相結合。是一種非交互性文本流編輯。
(1)調用sed的三種方式
使用sed命令行格式為:sed [options] sed命令 輸入文件
使用sed腳本文件格式為:sed[options] -f sed腳本文件 輸入文件
sed腳本文件[options] 輸入文件
--不管是使用shell命令行方式或腳本文件方式,如果沒有指定輸入文件,sed從標準輸入中接受輸入,一般是鍵盤或重定向結果。
(2)sed 命令的options如下
-n:不打印
-c:下一命令是編輯命令
-f:如果正在調用sed腳本文件
(3)sed在文件中查詢文本的方式
--使用行號,可以是一個簡單的數字,或是一個行號的范圍
--使用正則表達式
(4)讀取文本的方式
x x為一行號
x,y 表示行號范圍從x到y
/pattern/ 查詢包含模式的行
/pattern/pattern/ 查詢包含兩個模式的行
pattern/,x 在給定的行號上查詢包含模式的行
x,/pattern/ 通過行號和模式查詢匹配行
x,y! 查詢不包含指定行號x和y的行
(5)基本sed編輯命令
p 打印匹配行
d 刪除匹配行
= 顯示文件行號
a\ 在定位行號后附加新文本信息
i\ 在定位行號后插入新文本信息
c\ 用新文本替換定位文本
s 使用替換模式替換相應模式
r 從另一個文件中讀文件
w 寫文本到一個文件
q 第一個模式匹配完成后推出或立即退出
l 顯示與八禁止ASCII代碼等價的控制字符
{} 在定位行執行的命令組
n 從另一個文件中讀文本下一行,并附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
(6)舉例說明:
sed -n '2p' test.txt 打印第二行的信息(注意:-n是不打印不匹配的信息,若沒加-n,則打印文件的所有信息而不是匹配信息)
sed -n '1,4p' test.txt 打印第一行到第四行的信息
sed -n '/los/p' test.txt模式匹配los,并打印出來
sed -n '2,/los/p' test.txt 從第二行開始。。知道匹配第一個los
sed -n '/^$/p' test.txt 匹配空行
sed -n -e '/^$/p' -e '/^$/=' test.txt 打印空行及行號
sed -n '/good/a\morning' test.txt 在匹配到的good后面附加morning
sed -n '/good/i\morning' test.txt 在匹配到的good前面插入morning
sed -n '/good/c\morning' test.txt 將匹配到的good替換成morning
sed '1,2d' test.txt 刪除第1和2行
sed 's/good/good morning/g' test.txt 匹配good并替換成goodmorning
send 's/good/& hello /p' test.txt 匹配到good就在其后面加上hello
send 's/good/ hello &/p' test.txt 匹配到good就在其前面加上hello
? 6 find
6.1 查找目錄下的所有文件中是否含有某個字符串
find .|xargs grep -ri "context"
6.2 find詳解
(1)查找具有某些特征文件的命令,可遍歷當前目錄甚至于整個文件系統來查看某些文件或目錄,其遍歷大的文件系統時一般放在后臺執行。
(2)find命令的一般形式
find pathname -options [-print -exec -ok]
-pathname :find命令所查找的目錄路徑。如用"."來表示當前的目錄,用/來表示系統根目錄
-print :find命令將匹配的文件輸出到標準輸出
-exec: find命令對匹配的文件執行該參數所給出的shell命令,相應的命令形式為
'command'{} \; (注意{}和\之間的空格)
-ok 和 -exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。
options有如下幾種:
-name :按照文件名查找文件
-perm :按照文件權限來查找文件
-user :按照文件屬主來查找文件
-group :按照文件所屬的組來查找文件
-mtime -n +n 按照文件的更改時間來查找文件,-n表示文件更改時間距現在n天以內,+n表示文件更改時間距現在n天以前。find命令還有-atime 和-ctime選項,但它們都和-mtime選項相似。
-size n[c]查找文件長度為n塊的文件,帶有c時表示文件長度以字節計。
-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在
-newer file1 !file2查找更改時間比文件file1新但比文件file2舊的文件
-depth 先查找指定目錄有無匹配文件,若無則再在子目錄中查找
-type 查找某一類型的文件,如
b :塊設備文件
d:目錄
e:字符設備文件
p;管道文件
l:符號鏈接文件
f:普通文件
(3)find命令舉例
find -name "*.txt" -print 查找txt結尾的文件并輸出到屏幕上
find /cmd ".sh" -print 查找/cmd目錄下所有sh文件,并輸出
find . -perm 755 -print 查找當前目錄下權限為755的文件,并輸出
find `pwd` -user root -print 查找當前目錄下屬主為root的文件,并輸出
find ./ -group sunwill -print 查找當前目錄下所屬主是sunwill的文件
find /var -mtime -5 -print 查找/var目錄下更改時間為5天內的所有文件
find /var -mtime +5 -print 查找/var目錄下更改時間為5天以前的所有文件
find /var -newer "myfile1" ! -newer "myfile2" -print 查找/var目錄下比myfile1新,但是比myfile2舊的所有文件。
find /var -type d -print 查找/var目錄下所有目錄
find /var -type l -print 查找/var目錄下所有的符號鏈接文件。
find . -size +1000000c -print 查找當前目錄下大于1000000字節的文件
find / -name "con.file" -depth -print 查找根目錄下有無"con.file",若無則在其子目錄中查找
find . -type f -exec ls -l {} \; 查找當前目錄下是否有普通文件,若有則執行ls -l
(4)xargs命令
在 使用find命令的-exec選項處理匹配到的文件時,find命令將所有匹配到的文件一起傳遞給exec。不幸的是,有些系統對能夠傳遞給exec的命 令長度有限制,這樣find命令運行幾分鐘之后就算出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs的用處所在,特別是與 find命令一起使用,exec會發起多個進程,而xargs會多個,只有一個
find ./ -perm -7 -print | xargs chmod o-w 查找權限為7的文件并傳遞給chmod處理