-
用途
head - output the first part of files
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.
-
语法格式
head [OPTION]... [FILE]...
-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 |
-
实例
1. -c, --bytes=[-]NUM
print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file 打印每个文件的前NUM个字节;以‘-’开头,打印除了每个文件的最后NUM个字节之外的所有字节。
renhl@verygood:~/usb$ echo ABCDEFGHIJABCDEFGHIJ > 2.txt
renhl@verygood:~/usb$ echo 01234567890123456789 > 1.txt
renhl@verygood:~/usb$ hexdump -C 1.txt #有21个字节
00000000 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 |0123456789012345|
00000010 36 37 38 39 0a |6789.|
00000015
renhl@verygood:~/usb$ hexdump -C 2.txt #有21个字节
00000000 41 42 43 44 45 46 47 48 49 4a 41 42 43 44 45 46 |ABCDEFGHIJABCDEF|
00000010 47 48 49 4a 0a |GHIJ.|
00000015
renhl@verygood:~/usb$ head -c 3 1.txt 2.txt #打印每个文件的前3个字节
==> 1.txt <==
012
==> 2.txt <==
ABCrenhl@verygood:~/usb$ head -c -3 1.txt 2.txt #打印每个文件的除了后3个字节之外的所有字节,注意0x0a换行也算在最后3字节之内
==> 1.txt <==
012345678901234567
==> 2.txt <==
ABCDEFGHIJABCDEFGH
2. -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 打印每个文件的前NUM个行;以‘-’开头,打印除了每个文件的最后NUM个行之外的所有行。
renhl@verygood:~/usb$ seq 1 20
renhl@verygood:~/usb$ for i in $(seq 65 85); do echo "$(printf "\x$(printf '%X\n' $i) $(($i-65))" )"; done > 2.txt
renhl@verygood:~/usb$ for i in $(seq 65 85); do printf "\x$(printf '%X\n' $i) %d\n" $(($i-65)); done > 2.txt
renhl@verygood:~/usb$ head -n 3 1.txt 2.txt #打印前3行
==> 1.txt <==
1
2
3
==> 2.txt <==
A 0
B 1
C 2
renhl@verygood:~/usb$ head -n -3 1.txt 2.txt #从开始到最后第3行
==> 1.txt <==
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
==> 2.txt <==
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
K 10
L 11
M 12
N 13
O 14
P 15
Q 16
R 17
renhl@verygood:~/usb$
3. -q, --quiet, --silent
never print headers giving file names 不打印带文件名的头部
renhl@verygood:~/usb$ head -n 3 1.txt 2.txt #打印带文件名的头部
==> 1.txt <==
1
2
3
==> 2.txt <==
A 0
B 1
C 2
renhl@verygood:~/usb$ head -n 3 -q 1.txt 2.txt #-q参数不打印带文件名的头部
1
2
3
A 0
B 1
C 2
renhl@verygood:~/usb$
4. -v, --verbose
always print headers giving file names 打印带文件名的头部
renhl@verygood:~/usb$ head -n 3 1.txt #不打印带文件名的头部
1
2
3
renhl@verygood:~/usb$ head -n 3 -v 1.txt #-v参数 打印带文件名的头部
==> 1.txt <==
1
2
3
renhl@verygood:~/usb$
5. -z, --zero-terminated
line delimiter is NUL, not newline 以NUL为行结尾
renhl@verygood:~/usb$ seq 1 5 | tr '\n' '\0' > 1.txt #作成用NUL间隔的1到5的数字
renhl@verygood:~/usb$ hexdump -C 1.txt
00000000 31 00 32 00 33 00 34 00 35 00 |1.2.3.4.5.|
0000000a
renhl@verygood:~/usb$ head -n 3 1.txt #取得前3行,由于默认\n为分隔符,所以只有1行
12345renhl@verygood:~/usb$
renhl@verygood:~/usb$ head -n 3 -z 1.txt # 使用NUL为换行符,取得前3行,所以是123
123renhl@verygood:~/usb$