ファイルの内容を表示するコマンドと言うとcatがありますが、catはファイルの内容を全て出力しますね。このページではファイルの一部だけを表示させるコマンドを紹介します。
 最初に紹介するコマンド「head」は名前から想像される通り、ファイルの先頭を表示するコマンドです。

Welcome to Darwin!
[localhost:~] hiro% cd terminaltest
[localhost:~/terminaltest] hiro% ls 
test.txt
[localhost:~/terminaltest] hiro% cat test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

 このような内容のファイル"test.txt"に対してheadを使うと先頭の10行を表示します。

[localhost:~/terminaltest] hiro% head test.txt
1
2
3
4
5
6
7
8
9
10

 10行もいらない、という場合には、「-n 数字」オプションで表示させたい行数を指定できます。

[localhost:~/terminaltest] hiro% head -n 2 test.txt
1
2

 次に紹介する「tail」はファイルの終わりの方を表示するコマンドですが、こちらにはheadよりも多くのオプションがあります。

[localhost:~/terminaltest] hiro% tail test.txt
11
12
13
14
15
16
17
18
19
20

 このように、オプション無しで実行すると、指定されたファイルの最後の10行を表示します。
 もちろん、headと同様に「-n」で表示する行数を指定できます。

[localhost:~/terminaltest] hiro% tail -n 2 test.txt
19
20

 最後の2行が表示されましたね。
 次に、「-r」オプションを与えてみましょう。

[localhost:~/terminaltest] hiro% tail -r test.txt
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

 挙動ががらっと変わりましたね。このように、最後の行から逆順に先頭まで表示されます。
 続いて「-c 数字」オプションを試してみましょう。これは指定したバイト数だけファイルの末尾を表示させます。

[localhost:~/terminaltest] hiro% tail -c 2 test.txt
0
[localhost:~/terminaltest] hiro% tail -c 3 test.txt
20

 このファイルの最後には実は改行が入っているので、このように「-c 2」で0+改行の2バイトが、「-c 2」で20 +改行の 3バイトが表示されました。
 同じくバイト単位で指定するオプションとして「-b 数字」があります。これは512バイトのブロック単位で指定するもので、例えば「-b 2」とするとファイル末尾の1024バイト分を表示します。少しファイルサイズの大きいもので試してみましょう。

[localhost:~/terminaltest] hiro% cd ~
[localhost:~] hiro% tail -b 1 Paper.txt
or n = 15 taxa (14 mammalian orders and an outgroup taxon). The two resulting trees
improve previously published trees and seem to be of biological relevance. On this
dataset, the geometric algorithm produced a tree whose score is 98.2% of the optimal
value on this input set (72.1% vs. 73.4%). This gives rise to the hope that the
geometric approach will prove viable even for larger cases where the the exponential,
dynamic programming approach is no longer feasible.

 また数字の指定で「+数字」とすると、先頭からその数字行/バイト/ブロック進んだところから末尾まで指定します。これとheadを組み合わせると、ファイルの特定の行だけを指定させる事も可能になります(簡単なコマンドの組み合わせで複雑な作業をさせると気分が良いですね)。

 [localhost:~] hiro% cd terminaltest
[localhost:~/terminaltest] hiro% tail -n +5 test.txt
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[localhost:~/terminaltest] hiro% tail -n +5 test.txt | head -n 2
5
6