Head Command in Linux operating system

Head Command in Linux

In this article, we are going to show you that how to Use the head Command in Linux. If you want to learn the basic knowledge of the head Command in Linux then this article is ideal for you.

In the Linux, the head command is basically used to print the first lines (by default 10 lines) of given file to standard output.

Head command in Linux


Head Command Syntax

The basic syntax of the head command is show as below:

$ head [OPTION]... [FILE]...

Here,

  • OPTION – Use the head options for different actions.
  • FILE – Use here file name. If no file specified then head will read the standard input.

Use the Head Command

If you used head command without any option, then it will show the first 10 lines :

$ head filename.txt

Display a Specific Number of Lines

If you want to display the specific the number of lines then use the -n (--lines) option followed by the integer.

$ head -n <NUMBER> filename.txt

For example, if you want to display the first 20 lines from a file filename.txt then run the below command:

$ head -n 20 filename.txt

The below command also display the same result :

$ head -20 filename.txt

To display a Specific Number of Bytes

Use the -c (--bytes) option to print a specific number of bytes:

$ head -c <NUMBER> filename.txt

For example, to print first 200 bytes of data from the file filename.txt:

$ head -c 200 filename.txt

To display Multiple Files

If you want use multiple files with the head command then it will print the first 10 lines of each provided files.

$ head filename1.txt filename2.txt

And if you want to print the first 30 lines of each provided files, then run below command :

$ head -n 30 filename1.txt filename2.txt

Use Head with Other Commands

The head command can be used with other commands by redirecting the standard output from other command using pipes.

For example,

ls -t | head -n 4

 The above command print the four most recently modified files or folders.


That’s all

If you face any error and issue in above steps , please use comment box below to report

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top