Display the First Lines of a Text File with head in Linux

Learn how to use the Linux head command to preview the beginning of files, select a line count, read multiple files, and limit piped output.

The Linux head command displays the beginning portion of a file or input stream. It is useful when you want to quickly inspect a file's format, headers, introductory records, or general contents without displaying the entire file.

A line is a unit of text that normally ends with a newline character. By default, head displays the first 10 lines.

Basic head syntax

head FILE

For example:

head notes.txt

This prints the first 10 lines of notes.txt to the terminal. The file is not changed; head is a viewing and preview command, not an editing command.

Choose how many lines to display

Use the -n option to request a specific number of lines. An option is a command-line flag that changes a command's behavior.

head -n 5 notes.txt

This displays the first five lines. The count can also be attached directly to the option:

head -n5 notes.txt

Both forms request five lines. A count smaller than the file length limits the output. If the count is larger than the number of lines in the file, head prints all available lines and does not invent extra output.

Common head command forms

Command formMeaningTypical use
head FILEPrints the first 10 lines of one file.Quickly preview a text file.
head -n NUMBER FILEPrints a specified number of initial lines.Control the size of a preview.
head FILE1 FILE2Prints the beginning of each named file.Compare or inspect several files.
COMMAND | head -n NUMBERSends command output to head and keeps only its first lines.Limit long generated output.

View multiple files

You can provide more than one file name in the same command:

head -n 3 file1.txt file2.txt

This displays the first three lines from each file. When multiple named files are read, typical head output includes file-name headers so you can identify which lines belong to which file.

head file1.txt file2.txt

With several files, the output is divided into sections. This differs from reading standard input, where there is no collection of named file arguments for head to label.

Read standard input with head

Standard input, often called stdin, is input supplied to a command through a pipe, redirection, or the terminal instead of through a file argument. The head command can read standard input as well as named files.

Use a pipe

A pipe is the | operator. It sends one command's output to another command's input.

ls -l | head -n 5

Here, ls -l generates a directory listing, and head -n 5 displays only the first five lines of that output. This is useful for limiting long command results to an initial preview.

Read redirected input

Input can also be redirected from a file:

head -n 5 < notes.txt

This reads notes.txt through standard input rather than passing the file name as an argument. In this form, the output is treated as standard-input output and does not use the named-file section headers used for multiple file arguments.

Practical examples

Preview a file with the default count

head notes.txt

Expected result: the first 10 lines of notes.txt appear in the terminal.

Preview a system-style text file

head -n 10 /etc/passwd

This displays the initial records of the specified readable file. The exact contents depend on the system.

Inspect several files

head -n 3 file1.txt file2.txt

Expected result: three initial lines from each file, normally preceded by headers identifying the file names.

Limit generated output

ls -l | head -n 5

Expected result: only the first five lines produced by ls -l are displayed.

head compared with tail

head reads from the start of its input. The related tail utility displays the ending portion instead.

CommandReads fromDefault outputTypical purpose
headThe beginningFirst 10 linesPreview headers, introductory records, or the start of output.
tailThe endLast 10 linesInspect recent records, endings, or the latest output.

Use head when the information you need is at the beginning. Use tail when you expect it at the end. Neither command edits the file.

Troubleshooting head

File does not exist

If the command reports that a file does not exist, the name or path may be incorrect, or the current directory may not be the one you expect.

pwd
ls
head notes.txt

Use pwd to check the current directory and ls to see its files. Then provide the correct file name or a full path.

Permission denied

A permission error means the current user cannot read the file. Choose a readable file or obtain appropriate access according to the system's access policy. Do not change permissions unless you understand the security consequences and have authorization.

Fewer lines than requested

This is normal when the file contains fewer lines than the requested count. head prints every available line and stops at the end of the input.

Multiple-file output is difficult to distinguish

When several files are supplied, read the file-name headers above each section. You can also run head on each file separately or request fewer lines to make the output easier to scan.

The final lines were expected

head reads from the beginning, so it is not the right command for the final lines of a file. Use tail when the required content is at the end.