Linux常用命令head命令
head 命令
参数说明
参数 | 参数说明 |
---|---|
-n | 指定显示行数 |
-c | 指定显示的字节数 |
-v | 总是显示文件名的文件头 |
-q | 不显示文件名的文件头 |
[root@localhost home]# 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=[-]K print the first K bytes of each file;
with the leading `-', print all but the last
K bytes of each file
-n, --lines=[-]K print the first K lines instead of the first 10;
with the leading `-', print all but the last
K lines of each file
-q, --quiet, --silent never print headers giving file names
-v, --verbose always print headers giving file names
--help display this help and exit
--version output version information and exit
K 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.
Report head bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'head invocation'
head默认参数
默认显示前十行数据
head file.txt
# 等同于 head -n 10 file.txt
# 等同于 head -10 file.txt
[root@localhost home]# cat file.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
[root@localhost home]# head file.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
[root@localhost home]# head -n 10 file.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
head -n参数
显示每个文件的前K 行内容;如果附加"-"参数,则除了每个文件的最后K行外 显示剩余全部内容
[root@localhost home]# head -n 3 file.txt # 显示file.txt文件的前3行
[root@localhost home]# head -n -3 file.txt # 显示除了文件最后3行外的所有内容
[root@localhost home]# head -n 3 file.txt file1.txt # 打印多个文件的内容(会显示每个文件的名字)
[root@localhost home]# head -n 3 file.txt # 显示file.txt文件的前3行
Line 1
Line 2
Line 3
[root@localhost home]# head -n -3 file.txt # 显示除了文件最后3行外的所有内容
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
[root@localhost home]# head -n 3 file.txt file1.txt # 打印多个文件的内容
==> file.txt <==
Line 1
Line 2
Line 3
==> file1.txt <==
Line 1
Line 2
Line 3
head -c参数
显示每个文件的前K 字节内容;如果附加"-"参数,则除了每个文件的最后K字节数据外 显示剩余全部内容
[root@localhost home]# head -c file.txt # 无效的字节数。
head: file.txt: invalid number of bytes
[root@localhost home]# head -c 4 file.txt # 按文件内容大小来打印,打印前4个字节的内容
Line
[root@localhost home]# head -c -4 file.txt # 打印除了最后4个字节的文件内容 (空格一般两个字节)
[root@localhost home]# head -c file.txt
head: file.txt: invalid number of bytes
[root@localhost home]# head -c 4 file.txt
Line[root@localhost home]# head -c -4 file.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line
head -q参数
不显示包含给定文件名的文件头
[root@localhost home]# head -q file.txt # 打印时不显示文件名称
# 等同于 head --quiet file.txt
# 等同于 head --silent file.txt
# 其实后面跟上--quiet,--silent都是一样的,都不会显示文件名称,和默认打印是一样的效果。
head -v参数
总是显示包含给定文件名的文件头
[root@localhost home]# head -n 3 -v file.txt # 打印时显示文件名称
# 等同于 head -n 3 --verbose file.txt
[root@localhost home]# head -n 3 -v file.txt # 打印时显示文件名称
==> file.txt <==
Line 1
Line 2
Line 3
[root@localhost home]# head -n 3 --verbose file.txt
==> file.txt <==
Line 1
Line 2
Line 3
head --version
显示版本信息并退出
[root@localhost home]# head --version # 显示版本信息并退出
head (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie and Jim Meyering.