[Shell command] tail でファイルの最終行だけを取得する

作成日: 2026年09月26日

tail コマンドの -n 1 を使用すると、入力の最終行だけを取得できます。

下記の例では、3行の入力から最後の orange だけを出力しています。

printf 'apple\nbanana\norange\n' | tail -n 1

実行結果は下記のとおりです。

orange

ファイルを指定する場合も同じです。

tail -n 1 fruits.txt

-n オプションには末尾から出力する行数を指定します。

参考

Shell command tail