head — the first lines of a file
The head command prints the beginning of a file or of command output — 10 lines by default, -n sets how many.
Cheat sheet
7 commands Printable version-
head file.txtthe first 10 lines -
head -n 3 file.txtthe first 3 lines -
head -n -5 file.txteverything except the last 5 lines -
head -c 100 file.txtthe first 100 bytes -
head -n 1 data.csva table's header line -
head -n 2 a.txt b.txt2 lines of each, with names -
ps aux --sort=-%mem | headthe top of a long output
Overview
head prints the start of a file and stops. With no options it prints 10 lines; with
-n N, N lines. With several names, each chunk gets a header:
Most often head sits at the end of a pipeline and trims another command's long
output: the largest files, the hungriest processes, the first matches. Its pair,
tail, shows the end of a file.
Options
4-n 3 and the short form -3 are the same. Full list — man head.
| Option | What it does | Example |
|---|---|---|
-n N |
Print the first N lines; -n -N prints all but the last N. | head -n 3 file.txt |
-c N |
Print the first N bytes. | head -c 100 file.txt |
-q |
Never print ==> name <== headers for several files. | head -q -n 1 *.csv |
-v |
Always print the name header, even for one file. | head -v -n 3 app.conf |
Nothing found. Try a shorter query.
Recipes
4The processes using the most memory
Peek at a file without opening it all
Lines N to M
Troubleshooting
4head: cannot open 'x' for reading: No such file or directory
There's no file at that path. Check the name with ls and the current directory
with pwd.
head: invalid number of lines
-n must be followed by a number: head -n 5, not head -n five or a bare head
-n.
The output stops in the middle of a line
You used -c (bytes) instead of -n (lines). With -c, head cuts by bytes, even
in the middle of a multibyte character.
See also
- tail — the last lines and following a log.
- cat — the whole file.
- less — page through a long file.
- text-tools — sort, uniq, wc and other pipeline pieces.
- grep — find lines by word rather than by number.
Found an inaccuracy? Report an error in the article