head command in Linux

In this tutorial, you’ll explore how to use the head command in Linux. In the world of Linux, a terminal is an essential tool that can be used to accomplish a variety of tasks. One of the most commonly used commands in Linux is the “head” command. The “head” command is used to display the first few lines of a file or the output of a command. This command is useful when you need to quickly look at the contents of a file or output without having to scroll through the entire document.

In this blog, we will explore the “head” command in Linux, its syntax, options, and some practical examples.

Syntax:

The syntax of the “head” command is straightforward. Here is the general syntax:

The syntax of the “head” command is straightforward. Here is the general syntax:

head [options] [file(s)]

Options:

The “head” command comes with several options that allow you to control how the output is displayed. Here are some of the most commonly used options:

  • -n: This option allows you to specify the number of lines you want to display. For example, if you want to display the first five lines of a file, you can use the following command:
head -n 5 file.txt
  • -c: This option allows you to specify the number of bytes you want to display. For example, if you want to display the first 100 bytes of a file, you can use the following command:
-c 100 file.txt
  • -v: This option is useful when you are using the “head” command to display the output of another command. By default, the “head” command displays the file name along with the output. If you don’t want to display the file name, you can use the “-v” option.
ls | head -v

Examples:

Now that we have covered the basics of the “head” command, let’s take a look at some practical examples.

Example 1: Display the first 10 lines of a file

To display the first 10 lines of a file named “file.txt”, you can use the following command:

head -n 10 file.txt

Example 2: Display the first 50 bytes of a file

To display the first 50 bytes of a file named “file.txt”, you can use the following command:

head -c 50 file.txt

Example 3: Display the first line of multiple files

To display the first line of multiple files, you can use the following command:

head -n 1 file1.txt file2.txt file3.txt

Example 4: Display the output of a command

You can use the “head” command to display the output of a command. For example, to display the first five lines of the output of the “ls” command, you can use the following command:

ls | head -n 5

Conclusion:

In conclusion, the “head” command is a powerful tool that can be used to quickly view the contents of a file or the output of a command. With its various options, you can control how the output is displayed and easily extract the information you need. Whether you are a beginner or an experienced Linux user, the “head” command is an essential tool that you should be familiar with, see the man page for the head command in Linux.

I hope this article has been helpful in understanding the head command in Linux. Thank you for reading!

If you have any questions please leave them in the comments below

 

Leave a Reply

Your email address will not be published. Required fields are marked *