常用方法:
实时日志:tial -f xxx.log
搜索含关键字日志:cat -n filename | grep "关键字"
常用命令:
tail:
-n:显示符号,例
tail -100f test.log 实时监控100行日志
tail -n 10 test.log 查询日志尾部最后10行日志
tail -n +10 test.log 查询首10行之后的所有日志
head:
head -n 10 test.log 查询日志文件中头10行日志
head -n -10 test.log 查询除了最后10行以外的其他所有日志
cat:
cat -n test.log | grep "关键字" 查询含关键字的日志
vim:
1、进入vim编辑模式:vim filename
2、输入"/关键字",按enter查找
3、查找,按"n" ( n 查找上一处错误日志 N 查找下一处错误日志)
退出:esc --> :wq! 保存退出 :q! 不保存退出
less:
1、less test.log
2、shift + G 到文件尾部,输入 ? ,加上想要搜索的关键字,例: ? 关键字
3、shift + n 在关键字之间切换
按行号查看:
1、cat -n test.log | grep "关键字" 得到关键日志的行号
2、cat -n test.log | tail -n +100 | head -n 20 从第100行开始往后查20条记录
tail -n +100 表示查找第100行之后的日志
head -n 20 表示在前面的查询结果中在查前20条记录
按日期查找:
sed -n '/日志打印的日期/p' test.log
例:sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
通过grep '日志打印的日期' test.log确认日志中是否有该时间点
分页/保存文件查看:
1、cat -n test.log | grep "关键字" | more 可以分页打印,通过空格翻页
2、cat -n test.log | grep "关键字" >xxx.txt 保存到xxx.txt文件中