linux日志查看
有如下命令,分别总结下:
tail,head,cat,tac,sed,less,echo
1.tail
命令格式 tail [必要参数][选择参数][文件]
-f 循环读取
-v 显示详细的处理信息
-c <数目>显示字节数
-n<行数>显示行数
-q,–quiet,–slient从不输出给出文件名的首部
-s,—sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒
tail -n 100 catalina.out 查询日志尾部最后100行的日志;
tail -n +100 catalina.out 查询100行之后的所有日志;
tail -fn 100 catalina.out 循环实时查看最后100行记录(最常用的)
配合着grep用,例如:tail -fn 100 catalina.out | grep – ‘关键字’
如果一次性查询的数据量太大,可以进行翻页查看,
例如:tail -n 6000 catalina.out | more -100 可以进行多屏显示(ctrl +f 或者 空格键可以快捷键)
2.head
head -n 1000 catalina.out //查询日志文件中的头10行日志;
head -n -1000 catalina.out //查询日志文件中的除了最后10行的日志;
head其他参数与tail类似
NAME(名称)
head - output the first part of files
输出文件开始的部分
SYNOPSIS(概要,大纲)
head [OPTION]... [FILE]...
DESCRIPTION(描述)
Print the first 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.
将每个文件的开始10行打印到标准输出。
如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头。
如果没有指定文件,或者文件为-,那么就从标准输入上读取。
Mandatory arguments to long options are mandatory for short options too.
长选项必须用的参数在使用短选项时也是必须的。
-c, --bytes=[-]K
print the first K bytes of each file;
with the leading '-', print all but the last K bytes of each file
输出开始的K个字节;
或者使用 -c -K,从每个文件的开始一直到倒数K个字节
-n, --lines=[-]K
print the first K lines instead of the first 10;
with the leading '-', print all but the last K lines of each file
输出开始的K行,而不是开始的10行;
或者使用 -n -K,从每个文件的开始一直到倒数K行
-q, --quiet, --silent
never print headers giving file names
当有多个文件参数时,不输出各个文件名;
-v, --verbose
always print headers giving file names
当有多个文件参数时,总是输出各个文件名;
--help display this help and exit
显示此帮助信息并退出
--version
output version information and exit
显示版本信息并退出
3.cat
cat是由第一行到最后一行连续显示在屏幕上
$cat filename //一次显示整个文件
$cat >filename //从键盘创建一个文件
$cat file1 file2 >file //将几个文件合并为一个文件,只能创建新文件,不能编辑已有文件
$cat -n textfile1 > textfile2 //将一个日志文件的内容追加到另一个;
$cat : >textfile2 //清空一个日志文件
注意: >是创建 >>是追加
cat的其他参数与tail类似
4.tac
tac则是由最后一行到第一行反向在荧幕上显示出来
5.sed
这个命令可以查找日志文件特定的一段,也可以根据时间一个范围查询
//按照行号
sed -n ‘2,100p’ catcalina.out //这样你就可以只查看文件的第5行到第10行
//按照时间段
sed -n ‘/2019-01-17 10:07:10/,2019-02-14 16:54:05/p’ catalina.out
6.less
less log.log
shift+G命令到文件尾部 然后输入? 加上你要搜索的关键字 eg: ?1213
shift+n 关键字之间进行切换
7.echo
输出 echo “13234”
附录:
一、tomcat运行日志
1.先切换到:cd usr/local/tomcat3/logs
2.tail -f catalina.out
3.这样运行时就可以实时查看运行日志了
crtl+c是退出tail命令。
alt+E+R重置
二、linux日志文件说明
/var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一
/var/log/secure 与安全相关的日志信息
/var/log/maillog 与邮件相关的日志信息
/var/log/spooler 与UUCP和news设备相关的日志信息
/var/log/cron 与定时任务相关的日志信息
/var/log/boot.log 守护进程启动和停止相关的日志消息
/var/log/wtmp 该日志文件永久记录每个用户登录、注销及系统的启动、停机的事件