tail命令使用

3,142次阅读
没有评论

共计 1634 个字符,预计需要花费 5 分钟才能阅读完成。

tail 命令从指定点开始将文件写到标准输出. 使用 tail 命令的 - f 选项可以方便的查阅正在改变的日志文件,tail -f filename 会把 filename 里最尾部的内容显示在屏幕上, 并且不但刷新, 使你看到最新的文件内容. 

1.命令格式;

tail[必要参数][选择参数][文件]

2.命令功能:

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

3.命令参数:

-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

-c< 数目 > 显示的字节数

-n< 行数 > 显示行数

–pid=PID 与 - f 合用, 表示在进程 ID,PID 死掉之后结束. 

-q, –quiet, –silent 从不输出给出文件名的首部  

-s, –sleep-interval=S 与 - f 合用, 表示在每次反复的间隔休眠 S 秒  

4.使用实例:

实例 1:显示文件末尾内容

命令:

tail -n 5 test.log

输出:

[root@localhost test]# tail -n 5 test.log 

55555

66666

77777

88888

99999

==============================

[root@localhost test]#

说明:

显示文件最后 5 行内容

实例 2:循环查看文件内容

命令:

tail -f test.log

输出:

[root@localhost ~]# ping 192.168.120.204 > test1.log &

[root@localhost ~]# tail -f test1.log 

PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.

64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms

64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms

64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms

64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms

64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms

64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms

64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms

64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms

[root@localhost ~]#

说明:

ping 192.168.120.204 > test1.log & // 在后台 ping 远程主机。并输出文件到 test1.log;这种做法也使用于一个以上的档案监视。用 Ctrl+c 来终止。

实例 3:从第 5 行开始显示文件

命令:

tail -n +5 test.log

输出:

[root@localhost test]# cat test.log 

11111

22222

33333

44444

55555

66666

77777

88888

99999

==============================

[root@localhost test]# tail -n +5 test.log

55555

66666

77777

88888

99999

==============================

正文完
 
Winter
版权声明:本站原创文章,由 Winter 2016-04-22发表,共计1634字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码