几个关于 head 命令的实用例子

系统 Linux
今天我们通过例子就 head 命令介绍一些实际工作中可能会用到的知识。

我们在先前的文章中多次提到过 head 命令,它是在 Linux 终端中查看文本文件的一种方式,我们可以使用 head 命令从文件的开始部分打印指定行数的内容。

它的语法结构如下所示:

head [option] [filename]

今天我们通过例子就 head 命令介绍一些实际工作中可能会用到的知识。

关于 head 命令的一些例子

作为演示,我们使用如下内容的文本文件:​

The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

首先,在不使用任何选项的情况下,使用 head 命令查看文本文件,默认会显示文件的前 10 行内容,如下所示:​

$ head tiap.txt 
The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage

当然如果文件内容不足 10 行,那么就会显示整个文件的内容。

使用 head 命令打印文件前 n 行

使用 head 命令,打印文件的前 n 行,需要使用选项 -n,后面跟一个行数。比如,要打印文件多大前 3 行,可使用如下命令:​

$ head -n 3 tiap.txt 
The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links

打印除了最后 n 行以外所有的内容

上面例子中,行数如果为负值,比如 -3,那么就会打印除了最后 3 行以外所有的内容,如下所示:​

$ head -n -3 agatha.txt 
The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait

使用 head 命令打印多个文件

使用 head 命令可以同时查看多个文件,语法如下:

head -n N file1 file2 file3

比如,有两个文件,想要同时查看这两个文件的前两行,如下命令:​

$ head -n 2 tiap.txt sherlock.txt 
==> tiap.txt <==
The Mysterious Affair at Styles
The Secret Adversary
==> sherlock.txt <==
A Scandal in Bohemia
The Red-Headed League

如上面例子中的输出所示,每个文件的输出都会使用 =>filename<== 分隔。

处理输出中的头信息

上面的例子,其输出中带有文件名来分割不同文件的内容,如果不想看到这个分割信息(文件头信息),可以使用 -q 选项(quiet 模式)将头信息省略掉:​

$ head -q -n 2 tiap.txt sherlock.txt 
The Mysterious Affair at Styles
The Secret Adversary
A Scandal in Bohemia
The Red-Headed League

另外一个问题,你可能也注意到了,就是在打印单个文件的时候,其输出中是不带文件头信息的,可以使用 -v 选项强制让其打印文件名,如下所示:​

$ head -v -n 2 tiap.txt 
==> tiap.txt <==
The Mysterious Affair at Styles
The Secret Adversary

打印指定大小的字节/字符数

使用 head 命令还可以打印指定字节数的内容,使用 -c 选项来实现,后面跟字节数。

一般来说,一个字符的大小就是一个字节,所以也可以把它当作是打印一定数量的字符数。如下:​

$ head -c 3 tiap.txt 
The

与行数类似,使用 -c 选项后面指定一个负数,可以打印除了最后指定数量的字符以外其他的所有内容,如下所示:

head -c -50 tiap.txt

使用 head 和 tail 命令组合,打印文件的指定几行

前面我们介绍过一篇文章,如何在 Linux 命令行中显示某个文件中指定的几行文字

介绍了使用 head 命令和 tail 命令打印文件中的指定几行内容,大家感兴趣可以去看一下。

责任编辑:庞桂玉 来源: TIAP
相关推荐

2023-03-24 10:07:46

tail命令

2014-03-17 17:27:51

Linux mvLinux 命令

2023-02-02 14:06:00

history命令技巧

2020-07-22 13:50:39

shell命令前端

2016-09-26 14:40:25

Windows内网实用命令

2022-11-02 08:32:46

find 命令Linux

2018-08-03 11:07:52

dd命令备份Linux系统

2022-11-09 19:02:10

Linux

2020-02-17 11:54:18

网络路由器命令

2009-10-13 14:33:00

2023-05-04 12:39:27

GDB命令程序

2015-07-14 10:13:29

2017-04-10 18:45:47

2018-02-25 10:45:08

Linux命令uptime

2018-02-24 14:00:42

TensorFlow数学计算机器学习

2023-04-20 13:59:01

Pythonwhile循环的

2010-07-14 16:09:52

Telnet命令例子

2020-04-14 09:59:46

新基建数据中心5G网络

2017-03-13 16:48:05

Git技巧

2010-04-29 12:46:42

Oracle SQL
点赞
收藏

51CTO技术栈公众号