GNU/Linux >> Linux の 問題 >  >> Linux

例を含むLinuxヘッドコマンド

headコマンドは、テキストファイルの最初の数行を印刷するために使用されます。デフォルトでは、headコマンドは、各ファイルの最初の10行を標準出力(表示画面)に出力します。他のコマンドと一緒に使用して、システムからより具体的な結果を提供できます。

前回の記事では、tailコマンドとそのさまざまなオプションの使用方法を学びました。この記事では、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 、または--silent スイッチ。

$ 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を使用します。 または--verbose スイッチ。

複数のファイルの最初の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]

#getaddrinfo(3)。##の設定これはDebianのhdparmのデフォルト設定です。これは#「order」行は古いバージョンのCライブラリでのみ使用されます。##カーネルイメージ管理overrides#include/etc/ld.so.conf.d/*.confdefault_driver=pulse#これは設定ファイルですlibauditの調整可能ファイル用。

5)ファイルの最後のNバイトを印刷する

ファイルの最後のNバイトを出力するには、-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を指定できます 、MBM 、など

$ 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:

または、-c +Nを使用することもできます 各ファイルのN番目から始まるバイトを出力する

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

最後のN行を削除して他のすべてを印刷するには、次のコマンドを使用します。

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

6)パイプ付きヘッドの使用

headコマンドは、他のコマンドとの間でパイプ処理できます。このコマンドは、ログファイルの内容を一覧表示し、それをheadにパイプして、ヘッダー付きの最初の5行を表示します。次に、sortコマンドは結果を逆の順序で表示します。

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

次の例では、bootstrap.logファイルの一部、つまり10行目から20行目までを出力します。これは、-n20 を意味します スイッチは最初の20行を出力し、-n10 スイッチは最初の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コマンドとその引数がどのように機能するかを理解する必要があります。テールとは正反対の方法で機能し、パイプを使用して他のコマンドでも使用できます。詳細については、ヘッドコマンドのマニュアルページを参照してください。

関連項目:

  • Linuxのその他のコマンドを例で説明

Linux
  1. 例を含むwcLinuxコマンド

  2. 例を含むLinuxTeeコマンド

  3. LinuxでのJQコマンドと例

  1. Linuxのソートコマンドと例

  2. Linuxでのエコーコマンド(例付き)

  3. Linuxでのmanコマンドと例

  1. LinuxでのAWKコマンドと例

  2. Linuxでの8つのヘッドコマンドの例

  3. LinuxでのCurlコマンドと例