Linux命令(16):tail

来源:互联网 发布:js自动刷新页面 编辑:程序博客网 时间:2024/05/06 22:18

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

命令格式:

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

命令功能:

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

命令参数:

  • -f 循环读取
  • -q 不显示处理信息
  • -v 显示详细的处理信息
  • -c<数目> 显示的字节数
  • -n<行数> 显示行数
  • –pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.
  • -q, –quiet, –silent 从不输出给出文件名的首部
  • -s, –sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

    例1.显示文件末尾内容
    命令:tail -n 5 mt1.txt (或者tail -5 mt1.txt)

[mt555@localhost Desktop]$ cat -n mt1.txt      1  hello     2  haw     3  are     4  you     5  waht     6  is     7  your     8  name     9  my    10  name    11  is    12  mt    13  nice    14  to     15  meet    16  you    17  [mt555@localhost Desktop]$ tail -n 5 mt1.txt  # 显示文件末尾5行内容niceto meetyou[mt555@localhost Desktop]$ tail -5 mt1.txt niceto meetyou[mt555@localhost Desktop]$ 

例2.循环查看文件内容
命令:tail -f test.log

[mt555@localhost Desktop]$ ping -n 999 127.0.0.1> mt1.txt&[2] 5029  [mt555@localhost Desktop]$ tail -f mt1.txt 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.071 ms64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.077 ms64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.070 ms64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.068 ms64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.052 ms64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.068 ms64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.073 ms

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

例3.从第5行开始显示文件
命令:tail -n +5 mt1.txt

[mt555@localhost Desktop]$ cat -n mt1.txt      1  hello     2  haw     3  are     4  you     5  waht     6  is     7  your     8  name     9  my    10  name    11  is    12  mt    13  nice    14  to     15  meet    16  you    17  [mt555@localhost Desktop]$ tail -n +5 mt1.txt wahtisyournamemynameismtniceto meetyou[mt555@localhost Desktop]$ 

例4.循环查看文件内容,每隔2秒更新一次
命令:tail -f -s 2 mt1.txt

[mt555@localhost Desktop]$ tail -f -s 2 mt1.txt 64 bytes from 127.0.0.1: icmp_seq=1938 ttl=64 time=0.054 ms64 bytes from 127.0.0.1: icmp_seq=1939 ttl=64 time=0.047 ms64 bytes from 127.0.0.1: icmp_seq=1940 ttl=64 time=0.373 ms64 bytes from 127.0.0.1: icmp_seq=1941 ttl=64 time=0.050 ms64 bytes from 127.0.0.1: icmp_seq=1942 ttl=64 time=0.052 ms64 bytes from 127.0.0.1: icmp_seq=1943 ttl=64 time=0.045 ms64 bytes from 127.0.0.1: icmp_seq=1944 ttl=64 time=0.044 ms64 bytes from 127.0.0.1: icmp_seq=1945 ttl=64 time=0.053 ms64 bytes from 127.0.0.1: icmp_seq=1946 ttl=64 time=0.052 ms64 bytes from 127.0.0.1: icmp_seq=1947 ttl=64 time=0.052 ms64 bytes from 127.0.0.1: icmp_seq=1948 ttl=64 time=0.047 ms
0 0