bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Bash/Text Processing
Bash•Text Processing

Bash head Command - Display the beginning of a file

Using the head Command

The head command is used to display the first part of files.

It's particularly useful for previewing the start of a file to understand its structure.

All examples below use the logfile.txt file:

line 01
line 02
line 03
line 04
line 05
line 06
line 07
line 08
line 09
line 10
...

Basic Usage

The head command displays the first 10 lines of a file by default:

Example: Display First 10 Lines

head logfile.txt
line 01
line 02
line 03
line 04
line 05
line 06
line 07
line 08
line 09
line 10

Options

The head command has several options used to customize its behavior:

  • -n [number] : Display the first [number] lines of the file.
  • -c [number] : Display the first [number] bytes of the file.

Option: -n [number]

The -n option allows you to specify the number of lines to display from the start of the file. By default, head shows the first 10 lines.

Example: Display First 5 Lines

head -n 5 logfile.txt
line 01
line 02
line 03
line 04
line 05

Option: -c [number]

The -c option allows you to display the first [number] bytes of a file instead of lines.

Example: Display First 20 Bytes

head -c 20 logfile.txt
line 01
line 02
line

Option: Multiple Files

The head command can be used to display the beginning of multiple files. By default, it prints the file name as a header before the content of each file.

Example: Display First 3 Lines of Multiple Files

head -n 3 logfile.txt logfile2.txt
==> logfile.txt <==
line 01
line 02
line 03
==> logfile2.txt <==
line 01
line 02
line 03

Option: -q

The -q option suppresses the printing of headers when multiple files are being processed. This is useful when you want to view the contents of multiple files without the file names being printed.

Example: Suppress Headers

head -q -n 3 logfile.txt logfile2.txt
line 01
line 02
line 03
line 01
line 02
line 03

Common Uses

The head command is commonly used to:

  • Preview the start of a file to understand its structure.
  • Quickly check the contents of a file without opening it fully.
  • Extract the header information from a data file.

Previous

Bash tail Command - Display Last Part of Files

Next chapter

System Monitoring

Start with Bash ps Command - Snapshot of Current Processes

This chapter

Overview
7

Lessons

18m

Read time

1. Bash grep Command - Search Text Using Patterns2. Bash awk - Pattern Scanning and Processing Language3. Bash sed Command - Stream Editor4. Bash cut Command - Remove Sections from Lines5. Bash sort Command - Sort Lines of Text Files6. Bash tail Command - Display Last Part of Files7. Bash head Command - Display the beginning of a file

On this page

Using the head CommandBasic UsageExample: Display First 10 LinesOptionsOption: -n [number]Example: Display First 5 LinesOption: -c [number]Example: Display First 20 BytesOption: Multiple FilesExample: Display First 3 Lines of Multiple FilesOption: -qExample: Suppress HeadersCommon Uses