Linux Command Reference ยท Viewing and Searching Files
Linux head command
Reference for the Linux head command: print the first lines or bytes of files, skip the tail with negative counts, and combine head with pipes.
head prints the beginning of its input. By default that means the first 10 lines of each file you name, or of standard input when you name none. It is the quickest way to peek at a large file, to check a CSV header, or to cut a long pipeline output down to its top entries.
You can ask for a different number of lines with -n, or for a number of bytes with -c. With several files, head prints a small header line before each one so you can tell them apart.
head [OPTION]... [FILE]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
head [-n [-]NUM | -c [-]NUM] [-q | -v] [FILE]...
| Option | Description |
|---|---|
-n NUM, --lines=NUM |
Print the first NUM lines. A leading minus (-n -5) means "everything except the last 5 lines". |
-c NUM, --bytes=NUM |
Print the first NUM bytes. A negative number skips that many bytes at the end. |
-q, --quiet |
Never print file name headers. |
-v, --verbose |
Always print file name headers. |
-z, --zero-terminated |
Treat NUL, not newline, as the line separator. |
-NUM |
Old shorthand for -n NUM. |
NUM accepts multiplier suffixes such as K, M and G (powers of 1024) for -c.
Returns: 0 on success; 1 if a file cannot be read (other files are still processed).
Usage notes
- Standard input.
command | headreads the pipe. If you give-as a file name it also means standard input. - Early exit helps.
headcloses its input when it has enough, so the producer getsSIGPIPEand stops.yes | head -n 3finishes instantly instead of running forever. - Negative counts (
-n -N,-c -N) are a GNU extension. They are handy for "everything except the footer", but they must read the whole input to know where the end is, so they cannot stream endless data. - Bytes are not characters.
-ccan cut a multi-byte UTF-8 character in half. - Header format. With several files the header is
==> name <==, with a blank line between files. Scripts that parse the output should use-q. - A single line from the middle needs a pipeline:
head -n 5 file | tail -n 1prints line 5.sed -n 5pdoes it in one command. - Do not use head to cut a file in place.
head -n 5 f > femptiesfbefore it is read. Write to a temporary file, then rename. - BSD and macOS
headdoes not support negative counts.