说明

  head命令用于显示文件内容头部,默认为文件的前10行。

格式

  head [option] [filename]

常用参数说明
  • -n:指定显示的行数
  • -c:指定显示的字节数
  • -q:不显示包含给定文件名的文件头
  • -v:总是显示包含给定文件名的文件头
示例

1、不使用参数,只显示文件的前10行

[root@localhost ~]# head /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports

2、指定-n参数,显示指定的前n行

[root@localhost ~]# head -3 /etc/services 
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#

3、指定-c参数,显示指定的前n个字节

[root@localhost ~]# head -c 10 /etc/services 
# /etc/ser

4、显示多个文件

[root@localhost ~]# head -n 1 /etc/passwd /etc/gshadow
==> /etc/passwd <== #这里是文件头
root:x:0:0:root:/root:/bin/bash

==> /etc/gshadow <==
root:::

5、指定-q参数,不显示文件的文件头,跟上一个例子对比看

[root@localhost ~]# head -qn 1 /etc/passwd /etc/gshadow
root:x:0:0:root:/root:/bin/bash
root:::

6、指定-v参数,显示文件的文件头

[root@localhost ~]# head -vn 5 /etc/services 
==> /etc/services <==
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10