每天五分钟linux(15)-tail

tail

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.使用实例:

4.1 例一:显示文件末尾内容

命令:
tail -n 5 log2012.log
输出:

zgy@NLP ~/test $ cat log2012.log 
1-day1
2-day2
3-day3
4-day4
5-day5
6-day6
7-day7
8-day8
zgy@NLP ~/test $ tail -n 5 log2012.log
4-day4
5-day5
6-day6
7-day7
8-day8
zgy@NLP ~/test $ 

说明:
显示文件最后5行内容

4.2 例二:循环查看文件内容

命令:
tail -f test.log
输出:

zgy@NLP ~/test $ tail -f log.log
64 bytes from 119.75.213.61: icmp_seq=22 ttl=50 time=24.6 ms
64 bytes from 119.75.213.61: icmp_seq=23 ttl=50 time=28.4 ms
64 bytes from 119.75.213.61: icmp_seq=25 ttl=50 time=27.4 ms
64 bytes from 119.75.213.61: icmp_seq=26 ttl=50 time=28.0 ms
64 bytes from 119.75.213.61: icmp_seq=27 ttl=50 time=27.7 ms
64 bytes from 119.75.213.61: icmp_seq=28 ttl=50 time=23.8 ms

--- www.a.shifen.com ping statistics ---
28 packets transmitted, 26 received, 7% packet loss, time 27048ms
rtt min/avg/max/mdev = 20.905/26.577/30.008/1.763 ms
^C
zgy@NLP ~/test $ 

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

4.3 例三:从第5行开始显示文件

命令:
tail -n +5 log.log
输出:

zgy@NLP ~/test $ tail -n +5 log.log
64 bytes from 119.75.213.61: icmp_seq=4 ttl=50 time=25.3 ms
64 bytes from 119.75.213.61: icmp_seq=5 ttl=50 time=20.9 ms
64 bytes from 119.75.213.61: icmp_seq=6 ttl=50 time=25.6 ms
64 bytes from 119.75.213.61: icmp_seq=7 ttl=50 time=27.8 ms
64 bytes from 119.75.213.61: icmp_seq=8 ttl=50 time=26.9 ms
64 bytes from 119.75.213.61: icmp_seq=9 ttl=50 time=27.5 ms
64 bytes from 119.75.213.61: icmp_seq=10 ttl=50 time=26.0 ms
64 bytes from 119.75.213.61: icmp_seq=11 ttl=50 time=27.1 ms
64 bytes from 119.75.213.61: icmp_seq=12 ttl=50 time=30.0 ms
64 bytes from 119.75.213.61: icmp_seq=13 ttl=50 time=24.8 ms
64 bytes from 119.75.213.61: icmp_seq=14 ttl=50 time=27.7 ms
64 bytes from 119.75.213.61: icmp_seq=15 ttl=50 time=27.6 ms
64 bytes from 119.75.213.61: icmp_seq=16 ttl=50 time=26.2 ms
64 bytes from 119.75.213.61: icmp_seq=18 ttl=50 time=25.1 ms
64 bytes from 119.75.213.61: icmp_seq=19 ttl=50 time=27.4 ms
64 bytes from 119.75.213.61: icmp_seq=20 ttl=50 time=25.7 ms
64 bytes from 119.75.213.61: icmp_seq=21 ttl=50 time=26.3 ms
64 bytes from 119.75.213.61: icmp_seq=22 ttl=50 time=24.6 ms
64 bytes from 119.75.213.61: icmp_seq=23 ttl=50 time=28.4 ms
64 bytes from 119.75.213.61: icmp_seq=25 ttl=50 time=27.4 ms
64 bytes from 119.75.213.61: icmp_seq=26 ttl=50 time=28.0 ms
64 bytes from 119.75.213.61: icmp_seq=27 ttl=50 time=27.7 ms
64 bytes from 119.75.213.61: icmp_seq=28 ttl=50 time=23.8 ms

--- www.a.shifen.com ping statistics ---
28 packets transmitted, 26 received, 7% packet loss, time 27048ms
rtt min/avg/max/mdev = 20.905/26.577/30.008/1.763 ms
zgy@NLP ~/test $ 

转自http://www.cnblogs.com/peida/archive/2012/11/07/2758084.html


上一篇:Hadoop工具让数据仓库迁移更轻松


下一篇:增加网站流量的方法