Linux常用查看日志命令

2020-05-30  本文已影响0人  Jay_星晨

第一种:tail命令

查看实时变化的日志(比较耗内存)

常用:

tail -f filename (默认最后10行,如果要看指定多少行,带上"-n 行数"就ok)

[root@yun~]$ tail -f test.log  //test.log总共100行,第一行1,第二行2...第一百行100
91
92
93
94
95
96
97
98
99
100

如果要退出:按ctrl+c

其他:

tail -n 5 filename (显示filename最后5行)

[root@yun~]$ tail -n 5 test.log   //test.log总共100行,第一行1,第二行2...第一百行100
96
97
98
99
100[bigbig@yun-tomcat-content_06 ~]$ 

tail -n +95 filename (从第95行开始显示文件)

[root@yun~]$ tail -n +95 test.log    //test.log总共100行,第一行1,第二行2...第一百行100
95
96
97
98
99
100[root@yun~]$ 

第二种:cat命令

搜索关键字附近的日志

常用:

cat -n filename |grep "关键字"

[root@yun~]$ cat -n test.log |grep "95"
    95  95     //一个95表示是行号,第95行;另一个95表示搜索结果:95
[root@yun~]$ 

其他:

cat filename | grep -C 5 '关键字' (显示日志里匹配字串那行以及前后5行)

[root@yun~]$ cat test.log |grep -C 5 "95"
90
91
92
93
94
95   //此处应高亮显示
96
97
98
99
100
[root@yun~]$ 

cat filename | grep -B 5 '关键字' (显示匹配字串及前5行)

[root@yun~]$ cat test.log |grep -B 5 "95"
90
91
92
93
94
95   //此处应高亮显示
[root@yun~]$ 

cat filename | grep -A 5 '关键字' (显示匹配字串及后5行)

[root@yun~]$ cat test.log |grep -A 5 "95"
95  //此处应高亮显示
96
97
98
99
100
[root@yun~]$ 

第三种:vi/vim命令

常用:

1)进入编辑查找:vi/vim

a、进入vim编辑模式:vim filename;
b、输入"/关键字",按enter键查找;
c、查找下一个,按n即可。

2)退出:

ESC键后,接着再输入:号时,vim会在屏幕的最下方等待我们输入命令;
wq! 保存退出;
q! 不保存退出。

其他:

/关键字 注:正向查找,按n键,把光标移动到下一个符合条件的地方;
?关键字 注:反向查找,按shift+n键,把光标移动到下一个符合条件的。

上一篇 下一篇

猜你喜欢

热点阅读