Linux常用命令_head命令

本文详细介绍了Linux中的head命令,包括其基本功能和各种参数的用法,如-n指定显示行数,-c指定显示字节数,-v和-q控制文件头的显示。通过实例演示了如何显示文件的前几行或字节,以及如何结合使用不同的参数。head命令是Linux系统中用于查看文件头部内容的实用工具,对于日常系统管理和文件检查非常有用。
摘要由CSDN通过智能技术生成

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值