このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

【Linux】ファイルの先頭を表示する:headコマンド

head コマンドの概要

 「head」コマンドは、指定したファイルの先頭部分を表示するためのLinuxコマンドです。デフォルトでは、ファイルの最初の10行を表示しますが、オプションを使用することで表示する行数やバイト数をカスタマイズできます。複数のファイルを指定した場合、それぞれのファイルの先頭部分がファイル名とともに表示されます。

【構文】
head [オプション] [ファイル...]

  • [オプション]: 表示する行数やバイト数を指定するためのオプション。
  • [ファイル...]: 表示する対象のファイル。複数のファイルを指定することもできます。

主な用途

  • テキストファイルの内容を確認するために、先頭の数行を表示する。
  • ログファイルの先頭部分を確認する。
  • ファイルの内容を部分的に閲覧することで、内容の把握やエラーチェックを行う。

主なオプションと説明

以下の表に、head コマンドの主なオプションとその説明をまとめます。

オプション説明
-n先頭から表示する行数を指定します。K には表示する行数を指定します。例:-n 5 で最初の5行を表示します。
-c先頭から表示するバイト数を指定します。K には表示するバイト数を指定します。例:-c 20 で最初の20バイトを表示します。
-q複数のファイルを指定したときに、ファイル名を表示しません。
主なオプションと説明

コマンドの使用例と解説

以下に、head コマンドの具体的な使用例とその出力例を示します。

1.サンプルファイルの作成

まず、以下の内容を持つサンプルファイル 「example.txt」 を作成します。

・「nano example.txt」コマンドを実行します。

nanoエディタを起動して、「example.txt」ファイルを作成します。

user01@ubuntu-vm:~$ nano example.txt

以下の内容を入力します。

Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.
Line 11: This is the eleventh line.
Line 12: This is the twelfth line.

 入力したら、「Ctrl + O」キーを入力して「Enter」キーで保存、「Ctrl + X」キーでnanoエディタを終了させます。

2.デフォルトの使用方法

・「head example.txt」コマンドを実行します。

デフォルトでは、先頭から10行までが表示されます。

user01@ubuntu-vm:~$ head example.txt 
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.

3.表示する行数を指定

・「head -n 5 example.txtコマンドを実行します。

先頭から5行目まで表示されます。

user01@ubuntu-vm:~$ head -n 5 example.txt
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.

4.先頭から特定のバイト数を表示

・「head -c 20 example.txt」コマンドを実行します。

先頭から最初の20バイトを表示します。

user01@ubuntu-vm:~$ head -c 20 example.txt
Line 1: This is the 

5.複数のファイルの先頭を表示

サンプルとして別のファイル「another_example.txt」を作成します。

・「nano another_example.txt」コマンドを実行します。

nanoエディタを起動して、「another_example.txt」ファイルを作成します。

user01@ubuntu-vm:~$ nano another_example.txt

以下の内容を入力します。

First line of another file.
Second line of another file.
Third line of another file.
Fourth line of another file.

 入力したら、「Ctrl + O」キーを入力して「Enter」キーで保存、「Ctrl + X」キーでnanoエディタを終了させます。

・「head example.txt another_example.txt」コマンドを実行します。

 「example.txt」の先頭から10行、「another_example.txt」は4行しかないため、4行が表示されます。

user01@ubuntu-vm:~$ head example.txt another_example.txt
==> example.txt <==
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.

==> another_example.txt <==
First line of another file.
Second line of another file.
Third line of another file.
Fourth line of another file.

6.ファイル名を表示せずに複数のファイルの先頭を表示

・「head -q example.txt another_example.txt」コマンドを実行します。

ファイル名が表示されずに複数のファイルの先頭が表示されます。

user01@ubuntu-vm:~$ head -q example.txt another_example.txt
Line 1: This is the first line.
Line 2: This is the second line.
Line 3: This is the third line.
Line 4: This is the fourth line.
Line 5: This is the fifth line.
Line 6: This is the sixth line.
Line 7: This is the seventh line.
Line 8: This is the eighth line.
Line 9: This is the ninth line.
Line 10: This is the tenth line.
First line of another file.
Second line of another file.
Third line of another file.
Fourth line of another file.

7.演習で作成したファイルの削除

演習で使用したファイルを削除します。

・「rm example.txt another_example.txt」コマンドを実行し、「ls」コマンドで削除されたかどうかを確認します。

user01@ubuntu-vm:~$ rm example.txt another_example.txt
user01@ubuntu-vm:~$ ls
snap          テンプレート  ドキュメント  ピクチャ      公開
ダウンロード  デスクトップ  ビデオ        ミュージック

まとめ

 これらの演習を通じて、「head」コマンドを使うことで、ファイルの先頭部分を簡単に確認する方法が理解できたと思います。「head」コマンドは、ログファイルの確認やファイルの内容を素早く把握するのに非常に便利です。