每天一个linux命令(14):head 命令
head
与 tail
就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head
用来显示档案的开头至标准输出中,而 tail
想当然尔就是看档案的结尾。
命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bobo@ubuntu:~/test$ head --help
Usage: head [OPTION]... [FILE]...
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=[-]NUM print the first NUM bytes of each file;
with the leading '-', print all but the last
NUM bytes of each file
-n, --lines=[-]NUM print the first NUM lines instead of the first 10;
with the leading '-', print all but the last
NUM lines of each file
-q, --quiet, --silent never print headers giving file names
-v, --verbose always print headers giving file names
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/head>
or available locally via: info '(coreutils) head invocation'
命令功能
head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。
使用实例:
1
2
3
4
5
6
7
8
9
10
bobo@ubuntu:~/test$ cat test1.txt
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
this is line 8
bobo@ubuntu:~/test$
1
2
3
4
5
bobo@ubuntu:~/test$ cat test2.txt
hello
world
this is test2
bobo@ubuntu:~/test$
显示文件的前n行
通过-n
控制要输出的行数,默认输出前10
行
1
2
3
4
5
bobo@ubuntu:~/test$ head -n 3 test1.txt
this is line 1
this is line 2
this is line 3
bobo@ubuntu:~/test$
当行数前添加-
时,输出除了最后 n
行外的其他内容
1
2
3
4
5
6
7
bobo@ubuntu:~/test$ head -n -3 test1.txt
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
bobo@ubuntu:~/test$
-c
参数使用相同,只是显示的是前多少个字符,而不是多少行而已
显示文件名
打开多个文件时,默认会输出文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bobo@ubuntu:~/test$ head test1.txt test2.txt
==> test1.txt <==
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
this is line 8
==> test2.txt <==
hello
world
this is test2
bobo@ubuntu:~/test$
使用-q 会不输出文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
bobo@ubuntu:~/test$ head -q test1.txt test2.txt
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
this is line 7
this is line 8
hello
world
this is test2
bobo@ubuntu:~/test$
单个文件时,默认也不会输出文件名,但是可以用 -v
设置始终显示文件名