Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH

Bash head Command

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

ead -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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.