Linux online course

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

Learn how to use Linux head to preview the beginning of files, choose line or byte counts, process multiple files, and limit pipeline output.

The head command is a Unix/Linux utility that prints the initial part of a file or standard input. It is useful for quickly inspecting a file's format, headers, comments, or first records without opening the entire file.

You can give head one or more file names, or provide input through a pipe or redirection. Its normal output stream is standard output (stdout), which is displayed in the terminal unless you redirect it elsewhere.

Basic head syntax

To display the beginning of a named file, use:

head FILE

For example:

head notes.txt

When no output count is specified, head displays the first 10 lines. A line is a sequence of text ending with a newline character. If the file contains fewer than 10 lines, head displays the available lines.

Select a specific number of lines

The -n option requests an exact number of initial lines. A positive count starts at line 1 and continues toward the end of the file.

head -n 5 notes.txt

This displays lines 1 through 5. On commonly used Linux versions of head, the number can also be attached to the option:

head -n5 notes.txt

The separated form, -n 5, is often easier for beginners to read and is a good choice for scripts that should be clear.

GNU head and negative counts

GNU head also supports a negative line count. For example:

head -n -5 notes.txt

This prints every line except the last 5 lines. Negative counts are useful when you want to omit a known trailer, but they are a GNU extension and may not be available in every Unix implementation.

Read multiple files

Pass several pathnames to one invocation to preview them together:

head -n 3 january.log february.log

This displays the first three lines from each file. When more than one file is supplied, head normally prints a file header before each section so you can identify which output belongs to which file.

OptionMeaningExample
-n NUMBERPrint the first NUMBER lines.head -n 5 notes.txt
-c NUMBERPrint the first NUMBER bytes.head -c 32 data.bin
-qSuppress file headers when processing multiple files.head -q -n 2 one.txt two.txt
-vAlways print file headers, including when they would not otherwise be required.head -v -n 2 one.txt two.txt

Use -q when labels would interfere with further processing or when you already know the order of the files. Use -v when identifying each section is especially important.

Use head with standard input and pipelines

Standard input (stdin) is input supplied to a command from a pipe, redirection, or interactive source instead of from a named file. A pipeline uses the | shell operator to send one command's stdout to another command's stdin.

For example, preview the first eight lines of a process listing without creating an intermediate file:

ps aux | head -n 8

This intentionally limits output from a command that may produce many lines. You can also use input redirection:

head -n 4 < settings.conf

Here, the shell reads settings.conf and supplies it to head as stdin. Running head without a file name also reads from stdin, so it can receive data from another command or from an interactive source.

Lines versus bytes

Use -n when the unit you care about is a line. Use -c when you need a fixed number of bytes, which can be useful for inspecting the beginning of a large or non-line-based file.

head -c 32 data.bin

This outputs the first 32 bytes of data.bin. Byte output does not respect line boundaries: it may stop in the middle of a line. For text encoded with a multibyte character set such as UTF-8, it may also cut through a character sequence and produce invalid or visually corrupted text.

Choosing a file-preview command

TaskRecommended commandReason
View the beginning of a filehead FILEShows the first 10 lines by default and starts quickly.
View the end of a filetail FILEtail displays the ending portion of input.
Browse interactivelyless FILELets you move through a file instead of viewing only one fixed portion.
Extract a specific line rangesed -n 'START,ENDp' FILEPrints a selected range rather than only the beginning or end.

Use grep when you need to filter for matching text, and use wc when you need counts such as the number of lines or bytes. For example, wc -l notes.txt reports the file's newline-delimited line count.

Troubleshooting head

Cannot open the file

If the command reports that a file cannot be opened, check these common causes:

  • The pathname may be misspelled.
  • You may be running the command from a different directory.
  • Your user may not have permission to read the file.

Check the current directory and its contents with:

pwd
ls

Then use the correct relative or absolute path. Review the file permissions and use only authorized access when a file is protected.

The number of lines seems wrong

  • If you did not provide -n, the default is 10 lines.
  • The file may contain fewer lines than requested.
  • The final line may not end with a newline character.

Use wc -l to check the number of newline characters. Displayed visual rows and newline-delimited lines are not always the same, especially when long lines wrap in the terminal.

Multiple-file output is difficult to distinguish

You may have suppressed file headers with -q, or redirected and combined output with other command output. Use normal multiple-file output or add -v to force headers. If you need completely separate output, process each file in a separate command.

Safe interpretation of head output

The beginning of a file is only an inspection sample. It may show a header, comments, or representative records, but it does not prove that the rest of the file has the same format or content. For broader inspection, combine head with tools such as less, sed, grep, or wc.

For more Linux command-line fundamentals, see Linux and Bourne Again Shell Bash.

Exam-relevant summary

  • head FILE prints the first 10 lines by default.
  • head -n NUMBER FILE selects an initial line count; head -nNUMBER FILE is also commonly supported.
  • head -n NUMBER FILE1 FILE2 processes multiple files and normally adds file-identifying headers.
  • COMMAND | head -n NUMBER limits another command's stdout through a pipeline.
  • head -c NUMBER FILE counts bytes rather than lines.
  • tail is used to view the ending portion of input.