Linux Head命令示例

时间:2020-03-05 15:28:31  来源:igfitidea点击:

HEAD命令用于打印文本文件的前几行。
默认情况下,HEAD命令将每个文件的前10行打印到标准输出,这是显示屏幕。
它可以与其他命令一起使用,以提供系统的更具体和结果。

在上一篇文章中,我们了解了如何使用尾部命令及其不同的选项。

在本文中,我们将学习如何使用Linux Head命令读取任何文本文件的前几行,并且是最常见的选项。

1)使用具有标准输入的head

如果没有指定文件,或者指定文件是 - 时 ,则从标准输入流(stdin)而不是文件读取的Head命令。
这意味着它会抛出从键盘中键入的一切。
在这个例子中,我将输入单词,然后头部将显示我键入的内容。
输入后,将按Ctrl + C退出。

$head 
this
this
is
is
an
an
example
example
of
of
head
head
with no file specified 
with no file specified

2)查看文件的前10行

指定带头部的文件名打印出前10行。

$head /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command

3)查看文件的第一个n行

我们可以指定要以外的第10页以外显示的确切行数。
我们可以使用-n或者--lines开关来执行此操作,然后执行我们要打印的行数。

$head -n11 /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd/&& run-parts --report /etc/cron.hourly

4)显示多个文件

如果将多个文件作为参数给出,则它将显示前10行,并在每个文件之前,标题显示文件名。

$head /etc/crontab /etc/group
==> /etc/crontab <==
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
==> /etc/group <==
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:

如果我们不想打印标题,则可以使用"-Q",","--Quite"或者" - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - " - ""开关"。

$head --silent /etc/crontab /etc/group
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:

要始终打印标题,我们可以根据"文件名",或者使用"-V"或者" - 鼠标"开关。

要显示多个文件的第一个n行,我们可以使用以下命令。

$head -3 /etc/group /etc/fuse.conf /etc/passwd
==> /etc/group <==
root:x:0:
daemon:x:1:
bin:x:2:
==> /etc/fuse.conf <==
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)
# Set the maximum number of FUSE mounts allowed to non-root users.
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

我们可以使用*通配符打印名称以特定分机结尾的每个文件的行。

$head -1 -q /etc/*.conf
# /etc/adduser.conf: `adduser' configuration.
# this file sets defaults for apg if apg is called without parameters
## Documents/brltty.conf. Generated from brltty.conf.in by configure.
# This file lists certificates that you wish to use or to ignore to be
# This is the main config file for debconf. It tells debconf where to
# /etc/deluser.conf: `deluser' configuration.
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)
[fwupd]
# Configuration for getaddrinfo(3).
## This is the default configuration for hdparm for Debian. It is a 
# The "order" line is only used by old versions of the C library.
## Kernel Image management overrides
#include /etc/ld.so.conf.d/*.conf
default_driver=pulse
# This is the configuration file for libaudit tunables.

5)打印文件的最后一个字节

要打印文件的最后一个字节,我们应该使用-c或者--bytes开关,后跟字节数。
这打印了文件的前50个字节。
你也可以使用

$head -c 70 /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:

要显示除字节之外的另一个大小,可以指定"KB","MB",""/"M",等等

$head -c 2kB /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
voice:x:22:

或者,我们可以在每个文件的第n个开始使用-c + n以输出字节

$head -c +50 /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:sy

要删除最后一个线条并打印其他一切,我们使用命令:

$head -n -20 /var/log/syslog

6)使用带管子的头部

头部命令可以向其管道或者来自其他命令。
此命令列出了日志文件的内容,将其向前移动以显示带有标题的前5行,然后排序命令以相反的顺序显示结果。

$cat /var/log/dpkg.log | head -n 5 -v | sort -r
==> standard input <==
2016-02-15 20:18:59 status unpacked base-passwd:amd64 3.5.39
2016-02-15 20:18:59 status unpacked base-passwd:amd64 3.5.39
2016-02-15 20:18:59 status half-installed base-passwd:amd64 3.5.39
2016-02-15 20:18:59 startup archives install
2016-02-15 20:18:59 install base-passwd:amd64 <none> 3.5.39

以下示例打印bootstrap.log文件的一部分,例如:从第10行到第20行。
这意味着-N20开关在第一个20行打印前20行,打印初始20线的最后10行。

$head -n20 /var/log/bootstrap.log | tail -n 10
 dpkg: warning: parsing file '/var/lib/dpkg/status' near line 4 package 'dpkg':
 missing maintainer
 dpkg: warning: parsing file '/var/lib/dpkg/status' near line 4 package 'dpkg':
 missing architecture
 Selecting previously unselected package base-passwd.
 (Reading database ... 0 files and directories currently installed.)
 Preparing to unpack .../base-passwd_3.5.39_amd64.deb ...
 Unpacking base-passwd (3.5.39) ...
 dpkg: base-passwd: dependency problems, but configuring anyway as you requested:
 base-passwd depends on libc6 (>= 2.8); however:

我们现在应该了解HEAD命令和其参数如何在显示文件的前几行。
它以完全相反的尾部起作用,也可以使用管道与其他命令一起使用。