head 显示文件开头若干行内容
-
功能:head命令显示文件开头若干行(默认10行)。
将每个指定文件的头10 行显示到标准输出。如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头。如果不指定文件,或者文件为"-",则从标准输入读取数据。语法:head [选项] [文件]
短选项 长选项 涵义 -c [-]K --bytes=[-]K 每个文件显示开头K字节。若使用了-K,则显示文件全部内容,除去最后K字节 -n [-]K --lines=[-]K 每个文件显示开头K行。若使用了-K,则显示文件全部内容,除去最后K行 -q --quiet 或 --silent 不显示包含给定文件名的文件头 -v --verbose 总是显示包含给定文件名的文件头(默认) 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 同样适用。head 实例
获取前多少字节内容
–c K ,获取前K个字节的内容。可以加上单位1(byte) 、1k(1KB)、1m(1MB)默认但是是字节[root@master lianxi]#head -c 10 test
-c 10 所以这里是10 byte,获取前两个字符(ASCII编码的时候)lwgarmstro[root@master lianxi]# [root@localhost test]#head -c 20 log2014.log
2014-01 2014-02 2014 [root@localhost test]#-c 1k 获取前1KB大小的内容,-c 1m 就是获取前1MB字节大小的内容[root@master lianxi]#head -c 1k /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 adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync......后面省略获取除去最后多少字节之外的内容-K,文件的除了最后K个字节以外的内容[root@localhost test]#head -c -32 log2014.log
2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12[root@localhost test]# 获取前多少行内容-n K ,比如 –n 3 就是获取前3行内容[root@master lianxi]#cat -n /etc/passwd | head -n 3
1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@localhost test]#cat log2014.log
2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12 ============================== [root@localhost test]#head -n 5 log2014.log
2014-01 2014-02 2014-03 2014-04 2014-05[root@localhost test]# 获取除去最后多少行之外的内容3)行号可以是负数,则表示从第一行开始到倒数第n –1 行,比如 –n –2 则表示从第一行到倒数第3行[root@master lianxi]# wc -l 1.lua 7 1.lua [root@master lianxi]#cat -n 1.lua | head -n -5
1 arr = {23,21,89,289,34,23,1,32,434,21,1} 2 [root@master lianxi]# [root@localhost test]#head -n -6 log2014.log
输出文件除了最后6行的全部内容2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07[root@localhost test]# 同时获取多个文件的前多上行或字节的内容<获取多个文件的前多少行或字节 的内容时候,默认会显示头部信息。 -v 显示头部信息(默认),-q 不显示头部信息[root@master lianxi]#head -v -n 1 1.lua 2.lua
==> 1.lua <== arr = {23,21,89,289,34,23,1,32,434,21,1} ==> 2.lua <== print("hello world") [root@master lianxi]#head -q -n 1 1.lua 2.lua
arr = {23,21,89,289,34,23,1,32,434,21,1} print("hello world")1)有这样的需求,有时会把文件的一些信息写在文件的前多少个字节,比如一些标志信息,写在文件头部,我们可以通过head –c 来获取前多少个字节的头部信息[root@master lianxi]#head -c 10 data
[datafile][root@master lianxi]#2)-n number,可以简写成 –number ,比如获取前5行,可以 –n 5 ,也可以 –5[datafile][root@master lianxi]#head -5 /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 adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin