6.head命令用法

226 阅读2分钟

六.head

1.命令介绍

head命令,输入文件管理命令之一,也是日常使用最频繁的命令之一。今天一起来探讨下head命令的使用 head命令的功能,主要是显示文本文档的开头内容,不加参数的时候默认显示前10行。

2.常用参数
[root@jichu ~]# head --help
用法:head [选项]... [文件]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -c<字节>或--bytes=[-]K 显示每个文件的前多少K字节内容,如果附加"-"参数,则除了每个文件的最后K字节数据外显示剩余全部内容。 
  -n<行数>或--lines=[-]K 显示每个文件的前K行内容,如果附加"-"参数,则除了每个文件的最后K行外显示剩余全部内容。
  -q, --quiet, --silent	不显示包含给定文件名的文件头
  -v, --verbose		总是显示包含给定文件名的文件头
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

K 后面可以跟乘号:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, 对于T, P, E, Z, Y 同样适用。
3.案例示范
3.1## 默认显示前10行

不加参数,默认显示前10行

[root@jichu test]# head /etc/passwd |wc -l
10
[root@jichu test]# 
3.2 指定显示前几行 -n 参数

显示文件的前3行

[root@jichu test]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@jichu test]# 
3.3 显示文件前10个字节
[root@jichu test]# head -c 10 /etc/passwd
root:x:0:0[root@jichu test]# 
3.4 显示除最后1个字节以外的内容
[root@jichu test]# head -c -1 /etc/passwd
3.5 其他用法

我们可以借助与其他命令一起,比如查询文件的第七行内容

[root@jichu test]# cat /etc/passwd |head -n 7|tail -n 1
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@jichu test]# 

head 命令,我们多用在查看文件的第几行, 以上就是关于head命令的总结, 也可以使用man head 查看更多参数。