5.11 查看文件前几个字节、几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字节。