Skip to content

Using head in Linux Commands

Published: at 12:00 AMSuggest Changes

Table of Contents

Open Table of Contents

Introduction

The head command is used to display the first few lines of a file in Linux. It is a useful tool for quickly previewing the contents of a file.

Basic Usage of head

Syntax

The basic syntax of head is:

head [options] [file...]

Example

To display the first 10 lines of a file:

head file.txt

Common Use Cases

  • Displaying a Specific Number of Lines: Use the -n option to specify the number of lines to display.
    head -n 5 file.txt
    

Advanced head Techniques

Displaying the Last Lines

Use the tail command instead of head to display the last lines of a file.

tail -n 5 file.txt

Conclusion

The head command is a simple yet effective tool for quickly previewing the contents of text files in a Linux environment.


Previous Post
Using grep in Linux Commands
Next Post
Exploring Linux Command History