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

By 

Updated on

5 min read

Using the head command in Linux to 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:

txt
head [OPTION]... [FILE]...
  • OPTIONhead options. The most common options are covered in the sections below.
  • FILE — Zero or more input file names. If no FILE is specified, or when FILE is -, head reads 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:

Terminal
head filename.txt
output
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

Display a Specific Number of Lines

Use the -n (--lines) option to specify the number of lines to display:

txt
head -n NUMBER filename.txt

To display the first 30 lines of filename.txt, run:

Terminal
head -n 30 filename.txt

You can omit the letter n and use just the hyphen and the number with no space between them:

Terminal
head -30 filename.txt

Both 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:

Terminal
head -n -5 filename.txt

Display a Specific Number of Bytes

The -c (--bytes) option prints the specified number of bytes from the beginning of the file:

txt
head -c NUMBER filename.txt

For example, to display the first 100 bytes of filename.txt:

Terminal
head -c 100 filename.txt

You can also append a multiplier suffix to the byte count:

SuffixMultiplierResult
b512head -c 2b → 1024 bytes
kB1000head -c 5kB → 5000 bytes
K1024head -c 5K → 5120 bytes
MB1 000 000head -c 2MB → 2 000 000 bytes
M1 048 576head -c 2M → 2 097 152 bytes

The following command displays the first 5120 bytes (5 KiB) of filename.txt:

Terminal
head -c 5K filename.txt

Display 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:

Terminal
head filename1.txt filename2.txt
output
==> 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:

Terminal
head -n 20 filename1.txt filename2.txt

Use 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:

Terminal
ls -lt | head -n 5

To show only the first line of a file, for example to read a CSV header row:

Terminal
head -1 filename.txt

To show the ten lowest values after sort ing a file numerically:

Terminal
sort -n filename.txt | head

To count the words in just the first 20 lines using wc :

Terminal
head -n 20 filename.txt | wc -w

To generate a random 24-character string by hashing the $RANDOM environment variable :

Terminal
echo $RANDOM | sha512sum | head -c 24; echo

Quick Reference

CommandDescription
head file.txtShow the first 10 lines
head -n 20 file.txtShow the first 20 lines
head -20 file.txtShow the first 20 lines (shorthand)
head -n -5 file.txtShow all lines except the last 5
head -c 100 file.txtShow the first 100 bytes
head -c 5K file.txtShow the first 5120 bytes
head file1.txt file2.txtShow first 10 lines of each file
command | head -n 20Limit command output to 20 lines
head -1 file.txtShow 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:

Terminal
grep "pattern" file.txt | head -n 5

Conclusion

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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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