每天一个Linux命令(15)tail命令
tail命令用于输入文件中的尾部内容。tail命令默认在屏幕上显示指定文件的末尾10行。
如果给定的文件不止一个,则在显示的每个文件前面加一个文件名标题。
(1)用法:
用法: tail [必要参数] [选择参数] [文件]
如果没有指定文件或者文件名为“-”,则读取标准输入。
(2)功能:
功能: 输出文件的末尾部分
(3)选项参数:
1) -n <k行数> 显示文件末尾k行内容
2) -c <k字节数> 显示文件末尾k个字节数
3) -f 循环读取
4) -q 不显示处理信息
5) -v 显示详细的处理信息
(4)实例:
1)[root@localhost Documents]# tail -n 5 ./tail_text 查看文件后5行的内容
[root@localhost Documents]# cat tail_text > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! [root@localhost Documents]# tail -n 5 ./tail_text > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
等价于tail -5 text_tail 查看后5行的内容
[root@localhost Documents]# tail -5 tail_text > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
2)[root@localhost Documents]# tail -n +5 tail_text 从第5行开始显示
[root@localhost Documents]# tail -n +5 tail_text > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
3)[root@localhost Documents]# head -n -5 tail_text与[root@localhost Documents]# tail -n -5 tail_text
[root@localhost Documents]# head -n 5 tail_text //显示文件前5行的内容 > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! [root@localhost Documents]# head -n -5 tail_text //除了文件后五行全部显示 > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! //head命令的-n参数,当后面的整数为正为负是有区别的 [root@localhost Documents]# tail -n 5 tail_text //tail命令的-n参数,当后面的整数为正为负是一样的 > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! [root@localhost Documents]# tail -n -5 tail_text //都是显示末尾的整数的绝对值行 > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
4)[root@localhost Documents]# tail -c 30 tail_text 显示末尾的字节数
[root@localhost Documents]# tail -c 30 tail_text n line! > 12 the twelve line! [root@localhost Documents]# tail -c -30 tail_text n line! > 12 the twelve line! [root@localhost Documents]# head -c 30 tail_text > 01 the first line! > 02 the [root@localhost Documents]# head -c -30 tail_text > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleve[root@localhost Documents]#
5)[root@localhost Documents]# tail -f tail_text 循环读取内容输出到标准输出
[root@localhost Documents]# tail -f tail_text //默认是后10行 > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! ^C [root@localhost Documents]# tail -f -n 12 tail_text //也可以自己指定 > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! ^Z [6]+ 已停止 tail -f -n 12 tail_text [root@localhost Documents]# tail -f -n 7 tail_text > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
当然,也可以把信息输入到文件中:
[root@localhost Documents]# tail -f tail_text>tempory ^Z [9]+ 已停止 tail -f tail_text > tempory [root@localhost Documents]# cat tempory > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line!
6)[root@localhost Documents]# tail -n +5 tail_text与[root@localhost Documents]# tail -n 5 tail_text
[root@localhost Documents]# tail -n +5 tail_text > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! [root@localhost Documents]# tail -n 5 tail_text > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! [root@localhost Documents]# head -n +5 tail_text > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! [root@localhost Documents]# head -n 5 tail_text > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line!
7)[root@localhost Documents]# tail -n +10 tail_text |head -n -2
[root@localhost Documents]# tail -n +10 tail_text //从第10行显示到尾部 > 10 the tenth line! > 11 the eleven line! > 12 the twelve line! [root@localhost Documents]# head -n -2 tail_text //除了末尾两行之外前面的都显示 > 01 the first line! > 02 the second line! > 03 the third line! > 04 the forth line! > 05 the fifth line! > 06 the sixth line! > o7 the seventh line! > 08 the eighth line! > 09 the nineth line! > 10 the tenth line! [root@localhost Documents]# tail -n +10 tail_text |head -n -2 //综合起来,用管道命令就是后一个命令处理前面的结果,因此达到只显示第10行的效果 > 10 the tenth line! [root@localhost Documents]#