head#
head 是包含在 GNU Coreutils 內的檔案開頭讀取工具。
head 是 Linux 系統中用於查看檔案「開頭部分」內容的指令。預設情況下,它會顯示檔案的前 10 行。這在檢查大型日誌檔、確認檔案格式或與管線(pipe)結合提取特定資料時非常有用。
Install#
$ sudo apt install coreutilsSetting in up#
Operate#
head [options] [file]
| 參數 | 範例指令 | 說明 |
|---|---|---|
-n | head -n 5 file | 指定行數。顯示檔案的前 N 行。 |
-n | head -n -5 file | 指定行數。顯示檔案的除了最後 N 行以外的所有內容。 |
-c | head -c 100 file | 指定位元組。顯示檔案的前 N 個位元組。 |
-q | head -q f1 f2 | 靜默模式。在讀取多個檔案時,不顯示包含檔名的標頭。 |
-v | head -v file | 詳細模式。總是顯示包含檔名的標頭。 |
-z | head -z -n 5 file | 零結尾。以 NUL 字元(而非換行符號)作為列分隔標誌。 |
| 格式 | 說明 | 範例 |
|---|---|---|
head -n [數字] | 顯示前 N 行 | head -n 15 config.conf |
head -c [數字] | 顯示前 N 字元 | head -c 50 raw_data.bin |
head -n -[數字] | 排除最後 N 行 | head -n -10 original.txt |
Reference#
Official docs: