🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

head Command

Beginner Text Processing man(1)

Output the first part of files

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
head [OPTION]... [FILE]...

What Does head Do?

The head command outputs the first part of files. By default, it prints the first 10 lines, but this can be customized. It is essential for quickly previewing file contents, examining log files, and processing data streams.

head works with both files and piped input, making it versatile in shell pipelines. It can output a specific number of lines or bytes, and can also output everything except the last N lines.

head is commonly paired with tail for viewing file beginnings and endings, and with sort for top-N queries like finding the largest files or most frequent log entries.

Options & Flags

OptionDescriptionExample
-n Output the first N lines (default 10) head -n 20 file.txt
-c Output the first N bytes head -c 100 binary.dat
-n -N Output all lines except the last N head -n -5 file.txt
-q Quiet mode — never print headers (for multiple files) head -q -n 1 *.txt
-v Always print headers with filenames head -v -n 5 *.log
N (shorthand) Short form for -n N head -20 file.txt

Practical Examples

#1 View first 10 lines (default)

Shows the first 10 lines of the syslog file.
$ head /var/log/syslog

#2 View first N lines

Shows the first 50 lines of the access log.
$ head -n 50 access.log

#3 View first N bytes

Reads the first 256 bytes from urandom and converts to base64.
$ head -c 256 /dev/urandom | base64

#4 Preview multiple files

Shows the first 3 lines of each .conf file with headers.
$ head -n 3 *.conf
Output: ==> nginx.conf <== worker_processes auto;

#5 Exclude last N lines

Shows everything except the last 2 lines. Useful for removing footers.
$ head -n -2 data.csv

#6 Top 5 largest files

Finds the 5 largest items in /var/log.
$ du -ah /var/log | sort -rh | head -5
Output: 2.1G\t/var/log/journal\n500M\t/var/log/syslog

Tips & Best Practices

Combine with tail for range: To extract lines 100-110: head -n 110 file | tail -n 11. Or use sed -n "100,110p" file for the same result.
head vs less: head is better for scripts and pipelines (non-interactive). less is better for interactive browsing with search and scrolling.
Binary files: Using head on binary files can mess up your terminal. If that happens, run the reset command to restore your terminal.

Frequently Asked Questions

How do I view the first N lines of a file?
Use head -n N filename. For example, head -n 20 /var/log/syslog shows the first 20 lines.
How do I get just the first line?
Use head -n 1 filename. This is useful for reading CSV headers: head -n 1 data.csv.
What is the difference between head and cat?
cat outputs the entire file, while head only outputs the first few lines. head is much more efficient for large files since it stops reading after the specified lines.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →