head和tail就像它们的名字一样浅显易懂,head头部,用于显示文件开头或者指定的区域,head用于获取文件开头信息至标准输出,而tail则是用于获取文件尾部信息至标准输出。


命令格式:

head [option] filename


命令参数:

-c :指定显示的字符数。

-n :指定显示的行数,不指定-n默认显示文件前10行。

-q :隐藏文件名称。

-v :显示文件名。


命令实例:

  1. 显示文件前10行。

    命令:head test.txt #不加参数,默认输出文件头10行。

    输出:

[root@oldboylinux ~]# head test.txt 
1
2
3
4
5
6
7
8
9
10
[root@oldboylinux ~]#

2.显示文件头15行。

  命令:head -n 15 test.txt

  输出:

[root@oldboylinux ~]# head -n 15 test.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@oldboylinux ~]#

3.显示文件的头20个字符

 命令:head -c 20 test.txt

 输出: #每行行尾存在一个换行符,可以在vi命令模式下,说用set list看到。

[root@oldboylinux ~]# head -c 20 test.txt    
1
2
3
4
5
6
7
8
9
10[root@oldboylinux ~]#