Home > Text Processing & Search > head

head: Display the beginning of a file

The head command outputs the beginning part (defaulting to the first 10 lines) of a file or standard input. It is useful for quickly understanding the content of large files by allowing you to specify the number of lines or bytes to output.

Overview

The head command is useful for quickly checking the top content of a text file. It is often used when reviewing the beginning of large log files or data files, and it outputs the first 10 lines by default.

Key Features

  • Outputs the beginning of a file (default 10 lines)
  • Allows specifying the number of lines or bytes to output
  • Can process multiple files simultaneously
  • Can be combined with other commands via pipes (|)

Key Options

The head command provides various options to control the beginning part of a file.

Output Control

Generated command:

Try combining the commands.

Description:

`head` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to effectively check file content through various usage examples of the head command.

Display the first 10 lines of a file (default)

head example.txt

Outputs the first 10 lines of the specified file.

Display the first 5 lines of a file

head -n 5 example.txt

Specifies the number of lines to output using the -n option.

Display the first 100 bytes of a file

head -c 100 example.txt

Specifies the number of bytes to output using the -c option.

Display the first 3 lines of multiple files

head -n 3 file1.txt file2.txt

When multiple files are specified, it outputs the first 3 lines of each file and displays the file names as headers.

Using with a pipe (|)

ls -l | head -n 5

Pass the output of another command to head to view only the top portion.

Display the first 2 lines of multiple files without headers

head -n 2 -q file1.txt file2.txt

Use the -q (quiet) option to suppress file name headers when processing multiple files.

Tips & Precautions

Tips and points to note for more efficient use of the head command.

Combination with tail command

By using head and tail together, you can extract a specific middle portion of a file.

  • Example: To output lines 11 through 20 of a file: `head -n 20 file.txt | tail -n 10`

Performance when processing large files

The head command reads only the beginning part of the file, so it operates quickly even on very large files. This is more efficient than other commands that need to read the entire file.

Caution with byte-unit output

When specifying bytes with the -c option, multi-byte characters (e.g., Korean characters) may be cut off in the middle, so caution is advised. In such cases, the characters may appear garbled.


Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands