Skip to content

How to Use head Command

homepage-banner

Introduction

This guide teaches you how to use the head command on Linux. head is a simple way to display the beginning of text files, which is useful for analyzing logs and other text files that change over time. It can also be combined with other tools for selective, real-time monitoring. When performing administrative tasks on Linux, head is one of the most useful tools available.

Usage

Here’s how to use head:

  1. Enter the head command followed by the file name you want to view:

    head /var/log/auth.log
    

    This will print the first ten lines of the /var/log/auth.log file to your terminal output.

  2. To change the number of lines displayed, use the n option:

    head -n 50 /var/log/auth.log
    

    In this example, the first 50 lines will be shown, but you can modify this number to show as few or as many lines as you need.

  3. To show the beginning of a file up to a specific number of bytes, you may use the c option:

    head -c 1000 /var/log/auth.log
    

    This will print the first 1000 bytes of the file to your screen. It can be useful in situations where a file must be broken into pieces of a fixed size (e.g., for uploading to a separate server).

  4. The head command can even be combined with other tools like grep to filter the results:

    head /var/log/auth.log | grep 198.51.100.1
    

    This command searches the first ten lines of your access log and only displays those containing the IP address 198.51.100.1. You can also apply options to head for an even more specific output.

These are just the basics of how to use head. It is an incredibly useful tool with many more options than we’ve listed here.

Leave a message