head Command in Linux: Display the First Lines of a File

The head command prints the first lines (10 by default) of one or more files or piped data to standard output.
This article explains how to use the Linux head command through practical examples and detailed explanations of the most common options.
head Command Syntax
The syntax for the head command is:
head [OPTION]... [FILE]...OPTION—headoptions. The most common options are covered in the sections below.FILE— Zero or more input file names. If noFILEis specified, or whenFILEis-,headreads from standard input.
How to Use the head Command
When used without any option, the head command displays the first ten lines of a file:
head filename.txtline 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10Display a Specific Number of Lines
Use the -n (--lines) option to specify the number of lines to display:
head -n NUMBER filename.txtTo display the first 30 lines of filename.txt, run:
head -n 30 filename.txtYou can omit the letter n and use just the hyphen and the number with no space between them:
head -30 filename.txtBoth commands produce the same result.
To display all lines except the last N, pass a negative number with -n. For example, to print all lines except the last five:
head -n -5 filename.txtDisplay a Specific Number of Bytes
The -c (--bytes) option prints the specified number of bytes from the beginning of the file:
head -c NUMBER filename.txtFor example, to display the first 100 bytes of filename.txt:
head -c 100 filename.txtYou can also append a multiplier suffix to the byte count:
| Suffix | Multiplier | Result |
|---|---|---|
b | 512 | head -c 2b → 1024 bytes |
kB | 1000 | head -c 5kB → 5000 bytes |
K | 1024 | head -c 5K → 5120 bytes |
MB | 1 000 000 | head -c 2MB → 2 000 000 bytes |
M | 1 048 576 | head -c 2M → 2 097 152 bytes |
The following command displays the first 5120 bytes (5 KiB) of filename.txt:
head -c 5K filename.txtDisplay Multiple Files
When multiple files are provided, head displays the first ten lines from each file. The output for each file is preceded by a header showing the file name:
head filename1.txt filename2.txt==> filename1.txt <==
...first 10 lines of filename1.txt...
==> filename2.txt <==
...first 10 lines of filename2.txt...You can combine this with other options. To show the first 20 lines of each file:
head -n 20 filename1.txt filename2.txtUse head in Pipelines
The head command is useful in pipelines to limit the output of other commands. Pipe the output of any command into head to see only the first few lines.
To show the five most recently modified files in a directory:
ls -lt | head -n 5To show only the first line of a file, for example to read a CSV header row:
head -1 filename.txtTo show the ten lowest values after sort
ing a file numerically:
sort -n filename.txt | headTo count the words in just the first 20 lines using wc
:
head -n 20 filename.txt | wc -wTo generate a random 24-character string by hashing the $RANDOM environment variable
:
echo $RANDOM | sha512sum | head -c 24; echoQuick Reference
| Command | Description |
|---|---|
head file.txt | Show the first 10 lines |
head -n 20 file.txt | Show the first 20 lines |
head -20 file.txt | Show the first 20 lines (shorthand) |
head -n -5 file.txt | Show all lines except the last 5 |
head -c 100 file.txt | Show the first 100 bytes |
head -c 5K file.txt | Show the first 5120 bytes |
head file1.txt file2.txt | Show first 10 lines of each file |
command | head -n 20 | Limit command output to 20 lines |
head -1 file.txt | Show only the first line |
FAQ
What is the difference between head and tail?head prints the first lines of a file; tail
prints the last lines. Use head to preview the beginning of a file and tail to inspect the most recent entries, such as the bottom of a log file.
How do I display only the first line of a file?
Use head -n 1 filename.txt or the shorthand head -1 filename.txt. This is useful for reading the header row of a CSV file or checking the first entry in a log.
How do I display all lines except the last N?
Pass a negative number to -n. For example, head -n -10 filename.txt prints all lines except the last ten. This works on GNU head (Linux); macOS head does not support negative values with -n.
Can I use head with grep
?
Yes. Pipe grep output through head to limit the number of matching lines shown:
grep "pattern" file.txt | head -n 5Conclusion
The head command is a fast way to preview the beginning of a file or limit the output of other commands in a pipeline. It pairs naturally with tail
, sort
, grep
, and wc
for quick text inspection and processing.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page