命令 tail 用于查看文件的末尾数据
在Linux系统中,命令 tail 用于查看文件的末尾数据,比如查看日志文件等等,默认显示指定文件的最后10行到标准输出,如果指定了多个文件,tail会在每段输出的开始添加相应的文件名作为头。与 cat 命令不同的是 tail 命令可以实时查看日志文件(一旦有日志内容生成会即时显示在终端)。
语法:
tail [参数] [文件]
参数:
-f 循环获取
-q 不显示处理信息
-v 显示详细的处理信息
-c<数目> 显示的字节数
-n<行数> 显示文件末尾n行内容
-q, --quiet, --silent 从不输出给出文件名的首部
-s, --sleep-intercval=S 与 -f合用,表示每次反复的时间休息S秒
案例:
显示test.log文件的最后行10行(不带参数默认显示后10行),注意区分cat与tail的区别
[root@VM_0_5_centos test]# ls test01.py test.log test.py [root@VM_0_5_centos test]# cat test.log # cat显示全部内容 s is line 1 this is line 2 this is line 3 this is line 4 this is line 5 this is line 6 this is line 7 this is line 8 this is line 9 this is line 10 this is line 11 this is line 12 [root@VM_0_5_centos test]# tail test.log # tail默认显示最后10行 this is line 3 this is line 4 this is line 5 this is line 6 this is line 7 this is line 8 this is line 9 this is line 10 this is line 11 this is line 12 [root@VM_0_5_centos test]#
实时显示文件末尾内容(如果文件内容在不断增长变化),在此不做演示
[root@master test]# tail -f test.log
上述实时显示命令执行后终端就不能输入其他命令了,会每隔一秒去检查一下文件是否增加新的内容,如果增加就追加在原来的输出后面并显示,处于一种实时监控输出文件的末尾内容的状态,直到按下(Ctrl + c)组合键才会停止。
显示test.log文件的末尾6行内容(注意:如果文件末尾是空行或空字符,那么也会算在里面)
[root@VM_0_5_centos test]# tail -n 6 test.log this is line 7 this is line 8 this is line 9 this is line 10 this is line 11 this is line 12 [root@VM_0_5_centos test]#
显示test.log文件的最后10个字符:
[root@VM_0_5_centos test]# tail -c 10 test.log s line 12 [root@VM_0_5_centos test]#