text
head
Outputs the first part of files. Defaults to the first ten lines but can trim by any line or byte count.
head [OPTION]... [FILE]... Common flags
| Flag | Purpose |
|---|---|
| -n | Print the first N lines (use -n -N to skip the last N) |
| -c | Print the first N bytes |
| -q | Never print filename headers with multiple files |
| -v | Always print filename headers |
Examples
head file.txt First 10 lines
head -n 50 log.txt First 50 lines
head -c 1K file.bin First 1024 bytes
head -n -5 report.txt All lines except the last 5
Gotcha
Old scripts sometimes use head -N (a single dash and number); modern GNU head still accepts it but prefer -n N for portability.
Related commands
tail
Prints the last part of files. Essential for log inspection, especially with the follow flag to stream new lines.
cat
Concatenates files and prints them to standard output. Also useful as a simple viewer or to feed content into pipelines.
wc
Counts lines, words, characters, and bytes in files or stdin. Frequently used at the end of pipelines to size results.
grep
Searches text for lines matching a regular expression. Central to log analysis and pipeline filtering.
sed
Stream editor that applies a small program to each line of input. Widely used for search-and-replace and inline file edits.