head用法

1 查看文件的前10行内容

head

如果你只需要查看一个文件的前10行内容,例如Chaucer的“Canterbury Tales”,就没有必要使用cat命令或less命令,使用head命令就可以了。head命令可以只打印出文件的前10行内容,然后返回到命令行。

$ head Canterbury_Tales.txt
Here bygynneth the Book of the Tales of Caunterbury

General Prologue

Whan that Aprill, with his shoures soote
The droghte of March hath perced to the roote
And bathed every veyne in swich licour,
Of which vertu engendred is the flour;
Whan Zephirus eek with his sweete breeth
Inspired hath in every holt and heeth
$

 

要快速浏览文本文件,head命令非常棒,即使文件很大也没有关系。只需要一眨眼的时间,你就能知道这个文件是否是你需要的文件。

2 查看多个文件的前10行内容

head file1 file2

可以用head命令一次查看多个文件的前10行内容。这听起来与cat命令有点相似,确实如此。不过,head命令能够提供一个标题(header)以分隔各个文件,这样就能够容易地区分各个文件。下面是Chaucer的“Canterbury Tales”和Milton的“Paradise Lost”前10行的内容。

$ head Canterbury_Tales.txt Paradise_Lost.txt
==> Canterbury_Tales.txt <==
Here bygynneth the Book of the Tales of Caunterbury

General Prologue

Whan that Aprill, with his shoures soote
The droghte of March hath perced to the roote
And bathed every veyne in swich licour,
Of which vertu engendred is the flour;
Whan Zephirus eek with his sweete breeth
Inspired hath in every holt and heath

==> Paradise_Lost.txt <==
Book I

Of Man's first disobedience, and the fruit
Of that forbidden tree whose mortal taste
Brought death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful seat,
Sing, Heavenly Muse, that, on the secret top
Of Oreb, or of Sinai, didst inspire
That shepherd who first taught the chosen seed

 

head命令自动用一个空行和标题来分隔两段摘录,这样就能够清楚区分不同的文件。

3 查看一个或多个文件的前几行内容

如果不想查看文件的前10行内容,可以使用-n选项,后面跟上一个数字,比如5(或者用--lines=5),让head命令显示不同行数的内容。如果你指定两个或更多的文件,例如Chaucer的“Canterbury Tales”和Milton的“Paradise Lost”,结果将显示所有文件的前几行内容。

$ head -n 5 Canterbury_Tales.txt Paradise_Lost.txt
==> Canterbury_Tales.txt <==
Here bygynneth the Book of the Tales of Caunterbury

General Prologue

Whan that Aprill, with his shoures soote

==> Paradise_Lost.txt <==
Book I

Of Man's first disobedience, and the fruit
Of that forbidden tree whose mortal taste
Brought death into the World, and all our woe,

 

注意,这5行包括空白行和文本行。5行就是5行,不管这5行包含什么内容。

4 查看文件前几个字节、几K字节或几M字节的内容

head -c

-n选项可以指定查看文件前面几行的内容,但是如果想查看一定字节数量的内容呢?或者几K字节的内容?甚至是几兆字节的内容(其实这样做挺傻,因为屏幕会不停地向上滚动)?可以使用-c(或--bytes=)选项。

要查看Chaucer的“Canterbury Tales”前100个字节的内容,可以使用以下命令:

$ head -c 100 Canterbury_Tales.txt
Here bygynneth the Book of the Tales of Caunterbury
General Prologue

Whan that Aprill, with his sh

 

100个字节就是100个字节,如果这种方法会把最后一个词从中间分开了,也只能就这样了。

要查看Chaucer的“Canterbury Tales”前100 KB的内容,可以使用以下命令:

$ head -c 100k Canterbury_Tales.txt
Here bygynneth the Book of the Tales of Caunterbury

General Prologue

Whan that Aprill, with his shoures soote
The droghte of March hath perced to the roote
And bathed every veyne in swich licour,
Of which vertu engendred is the flour;
Whan Zephirus eek with his sweete breeth
Inspired hath in every holt and heeth

 

而要查看Chaucer的“Canterbury Tales”前100 MB的内容,你可以使用……不!不要这样做,这可能把本书其余的内容都显示出来!但是如果你执意要这么做,可以试试以下的命令:

$ head -c 100m Canterbury_Tales.txt
 

这里的m表示1048576字节,或者是1024×1024字节。

你可能感兴趣的:(Linux,前端,linux)