每天一条Linux命令(8) 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@master test]# lstest.log[root@master test]# cat test.log    # cat显示全部内容this is line 1this is line 2this is line 3this is line 4this is line 5this is line 6this is line 7this is line 8this is line 9this is line 10this is line 11this is line 12[root@master test]# tail test.log   # tail默认显示最后10行 this is line 4this is line 5this is line 6this is line 7this is line 8this is line 9this is line 10this is line 11this is line 12[root@master test]#

实时显示文件末尾内容(如果文件内容在不断增长变化),这个不好演示

[root@master test]# tail -f test.log

上述实时显示命令执行后终端就不能输入其他命令了,会每隔一秒去检查一下文件是否增加新的内容,如果增加就追加在原来的输出后面并显示,处于一种实时监控输出文件的末尾内容的状态,直到按下(Ctrl + c)组合键才会停止。

显示test.log文件的末尾5行内容

[root@master test]# tail -n 5 test.log this is line 9this is line 10this is line 11this is line 12                   <====  # 这里的文件末尾有一行空内容[root@master test]#

显示test.log文件的最后10个字符

[root@master test]# tail -c 10 test.log  line 12                   <====  # 这里的文件末尾有一行空内容[root@master test]#

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bearx...

请作者喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值