linux命令之head

1.head介绍

linux命令head用来查看文件的前N行内容;默认head查看前10行

2.head用法

head [参数] 文件

head常用参数

参数

说明

-n

从头显示N行,默认显示10行,可以不写

-q

隐藏文件名,在查看两个及以上文件名的情况下有效

-v

显示文件名

-c

从头显示N字节的内容

3.实例

3.1.显示文件的前5行

命令:

head -n 5 a.txt

OR

head -5 a.txt

[root@centos79-3 ~]# head -n 5 a.txt 
1
2
3
4
5
[root@centos79-3 ~]# head -5 a.txt 
1
2
3
4
5
[root@centos79-3 ~]#

3.2.显示文件的前5个字节的内容

命令:

head -c 5 a.txt

OR

head -c5 a.txt

[root@centos79-3 ~]# head -c 5 a.txt 
1
2
3[root@centos79-3 ~]# head -c5 a.txt 
1
2
3[root@centos79-3 ~]#

3.3.显示输出内容的文件名

命令:

head -v a.txt

[root@centos79-3 ~]# head a.txt
1
2
3
4
5
6
7
8
9
10
[root@centos79-3 ~]# head -v a.txt
==> a.txt <==
1
2
3
4
5
6
7
8
9
10
[root@centos79-3 ~]#

3.4.显示除文件后两行的剩余内容

命令:

head -n -2 a.txt

[root@centos79-3 ~]# cat a.txt 
1
2
3
4
5
6
7
8
9
10
11
12
[root@centos79-3 ~]# head -n -2 a.txt
1
2
3
4
5
6
7
8
9
10
[root@centos79-3 ~]#

3.5.显示除文件后两个字符的剩余内容

命令:

head -c -2 a.txt

[root@centos79-3 ~]# cat a.txt 
1
2
3
4
5
6
7
8
9
10
11
12
[root@centos79-3 ~]# head -c -2 a.txt
1
2
3
4
5
6
7
8
9
10
11
1[root@centos79-3 ~]#

3.6.显示内容时隐藏文件的名称

命令:

head -n 5 -q a.txt b.txt

[root@centos79-3 ~]# head -n 5 a.txt b.txt 
==> a.txt <==
1
2
3
4
5

==> b.txt <==
1
2
3
4
5
[root@centos79-3 ~]# head -n 5 -q a.txt b.txt 
1
2
3
4
5
1
2
3
4
5
[root@centos79-3 ~]#