一、命令介紹
1、tail命令
????????tail命令是Linux系統中常用的命令之一,用于查看文件的末尾內容。它具有許多有用的選項,可以幫助用戶輕松地查找并顯示文件中的信息。 它默認顯示文件的最后10行,但可以通過各種選項來定制輸出的行數、字節數等。tail命令常用于監視和分析日志文件以及其他隨時間變化的文件。一些常用的tail命令選項包括:
- -n:用于指定要顯示的行數。例如,tail -n 20 filename.txt會顯示filename.txt文件的最后20行。
- -f:用于實時追蹤文件的更改并輸出新增的內容。這對于監視正在寫入的日志文件特別有用,如tail -f /var/log/syslog。
- -c:以字節為單位顯示指定范圍的內容。例如,tail -c 100M largefile.log會顯示largefile.log文件的最后100MB內容。
2、grep命令
?????? ·grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹配的行打印出來。grep命令的全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。grep命令可以通過各種命令行選項來增強其功能,例如使用-G、-E、-F選項來分別實現類似egrep和fgrep的功能。
????????grep命令在Linux系統中被廣泛應用,例如在系統日志文件中搜索特定的錯誤信息,或者在大量的文本文件中查找特定的內容。它也可以與其他命令結合使用,例如通過管道將某個命令的輸出作為grep的輸入,從而過濾出需要的信息。
二、命令組合的實例
????????使用tail命令的-f選項可以方便的查閱正在改變的日志文件,tail -f filename會把filename里最尾部的內容顯示在屏幕上,并且不但刷新,使你看到最新的文件內容。這個功能廣泛應用于程序調試以及流程跟蹤等場景,非常實用。
1. 實時跟蹤并過濾日志中的錯誤信息
tail -f /var/log/syslog | grep "error"? ?
tail -f /var/log/messages | grep "error"
????????這個命令會實時跟蹤系統日志文件 /var/log/syslog,并只顯示包含 "error" 字樣的行。這對于快速定位和處理系統錯誤非常有用。實際效果如下:
[root@ecs-52a1 serverlog]#[root@ecs-52a1 serverlog]# tail -f /var/log/messages | grep "error"Feb 21 22:00:29 ecs-52a1 dnf[3827294]:? File "/usr/lib64/python3.6/site-packages/libdnf/error.py", line 17, in <module>Feb 21 22:00:29 ecs-52a1 dnf[3827294]:??? _error = swig_import_helper()Feb 21 22:00:29 ecs-52a1 dnf[3827294]:? File "/usr/lib64/python3.6/site-packages/libdnf/error.py", line 16, in swig_import_helperFeb 21 22:00:29 ecs-52a1 dnf[3827294]:??? return importlib.import_module('_error')Feb 21 22:00:29 ecs-52a1 dnf[3827294]: ModuleNotFoundError: No module named '_error'^C[root@ecs-52a1 serverlog]#
2. 跟蹤特定用戶的登錄記錄
tail -f /var/log/auth.log | grep "username"
tail -f /var/log/secure | grep "username"
????????這個命令會跟蹤 /var/log/auth.log 文件(通常是認證日志文件),并只顯示包含特定用戶名 "username" 的行。這有助于監視特定用戶的登錄活動。實際效果如下:
[root@ecs-52a1 serverlog]#[root@ecs-52a1 serverlog]#[root@ecs-52a1 serverlog]# tail -n 100 /var/log/secure | grep "yunwei"Feb 21 21:20:00 ecs-52a1 sshd[3040976]: Accepted password for 121yunwei from 153.35.12.24 port 58840 ssh2Feb 21 21:20:00 ecs-52a1 sshd[3040976]: pam_unix(sshd:session): session opened for user 121yunwei by (uid=0)Feb 21 21:20:00 ecs-52a1 sshd[3041691]: Accepted password for 121yunwei from 153.35.12.24 port 17229 ssh2Feb 21 21:20:00 ecs-52a1 sshd[3041691]: pam_unix(sshd:session): session opened for user 121yunwei by (uid=0)Feb 21 21:20:09 ecs-52a1 su[3042919]: pam_unix(su:session): session opened for user root by 121yunwei(uid=1003)[root@ecs-52a1 serverlog]#
3. 分析 web 服務器訪問日志中的特定請求
tail -f /var/log/apache/localhost_access_log | grep "GET /specific-page"
????????這個命令會跟蹤 Apache web 服務器的訪問日志,并只顯示包含 "GET /specific-page" 字樣的行。這對于分析特定頁面的訪問情況非常有用。實際效果如下:
[root@ecs-52a1 serverlog]# tail -f /usr/apache/logs/localhost_access_log.2024-02-21.txt | grep "GET /webmgr/"
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/css/menu.css HTTP/1.1" 200 1575
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/msg/mymsg.js HTTP/1.1" 200 1432
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/script/LayuiWinUtils.js HTTP/1.1" 200 3152
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/script/LinkMap.js HTTP/1.1" 200 7234
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/script/prototypeExt/StringExt.js HTTP/1.1" 200 310
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/script/forbid.js HTTP/1.1" 200 2288
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/images/btnedit.png HTTP/1.1" 200 1099
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/images/btndelete.png HTTP/1.1" 200 1090
153.35.127.24 - - [21/Feb/2024:23:03:39 +0800] "GET /webmgr/layuiadmin/layui/lay/modules/laypage.js HTTP/1.1" 200 4472^C
[root@ecs-52a1 serverlog]#
4. 實時顯示某個進程的日志輸出
tail -f /var/log/processofmy.log | grep "important"
????????如果有一個名為 processofmy的進程,其日志輸出保存在 /var/log/processofmy.log 文件中,這個命令會實時跟蹤該日志文件,并只顯示包含 "important" 字樣的行。實際效果如下:
[root@ecs-52a1 serverlog]# tail -f ./sipserver.log | grep "Register"ProcAuthSuccForRegister:Get Ext Register<32050100001320000035@180.108.95.62:2145> Register KeepAlive Time<300>!ProcAuthSuccForRegister:Get Ext Register<32050100001320000202@180.108.95.62:2081> Register KeepAlive Time<300>!ProcRegister: DBTunnelSoap register successfully to <http://127.0.0.1:8081>.RegisterToUpperPlatform:ToSvr<123.60.149.23:9000 username=admin pass = xxxx>? Expires<3600>.RegisterToUpperPlatform:ToSvr<120.78.93.97:5060 username=32050100002000000001 pass = xxxxxxx>? Expires<3600>.^C[root@ecs-52a1 serverlog]#
5. 監視某個文件的變化,并過濾特定內容
tail -f /mypath/file.txt | grep "pattern"
????????這個命令會實時跟蹤 file.txt 文件的變化,并只顯示包含 "pattern" 字樣的行。這對于監視文件內容的變化并篩選感興趣的部分非常有用。
三、和其他命令組合的實例
1. 使用 tail、grep 和 wc 統計特定日志條目的數量
tail -n 1000 /var/log/app.log | grep "ERROR" | wc -l
????????這個命令會首先使用 tail 顯示日志文件的最后 1000 行,然后使用 grep 過濾出包含 "ERROR" 的行,最后使用 wc -l 統計這些行的數量,從而得知最近 1000 條日志中有多少個錯誤條目。
2.使用 tail、grep 和 sort 對日志進行排序
tail -n 1000 /var/log/access.log | grep "GET" | sort -r
????????這個命令會跟蹤訪問日志文件的最后 1000 行,過濾出所有包含 "GET" 請求的行,并使用 sort -r 將這些行按照逆序(從最新到最舊)排列,從而可以查看最近的 GET 請求。
3. 使用 tail、grep 和 awk 提取和處理特定字段
tail -f /var/log/syslog | grep "auth" | awk '{print $2, $3}'
????????這個命令會實時跟蹤系統日志文件,過濾出包含 "auth" 的行,然后使用 awk 提取每行的第二和第三個字段(默認字段分隔符是空格或制表符),從而僅顯示這些字段的內容。
4. 使用 tail、grep 和 sed 進行文本替換
tail -n 100 /var/log/messages | grep "warning" | sed 's/warning/ERROR/g'
????????這個命令會顯示日志文件中最后 100 行,過濾出包含 "warning" 的行,然后使用 sed 將這些行中的 "warning" 替換為 "ERROR"。這對于將警告級別提升為錯誤級別非常有用。