Use head command to print first part of files

Share this post to your friends !

This entry is part 8 of 10 in the series Linux commands: Part 6 - Redirection

WriteBash - In this article, I will introduce you to the head command. We use head command to print first part of files.

Sometimes, you don’t want to display the entire contents of the file on the terminal window. You just want to display the first few lines of the file.

About the head command

The head command will display the first 10 lines of the file. This is very beneficial in case the file content is too long.

In contrast to the head command, we have the tail command used to print the last 10 lines of the file.

Syntax of this command.

$ head [options] [file]

Example use head command to print first part of files

Use head command without options

Now that we will do an example, I will use the ls command and combine redirection > to print out the entire list of files in the /bin directory into the ls-bin.txt file.

$ ls /bin > ls-bin.txt

And we count the number of lines with wc command, you will see that the file has 177 lines.

$ wc -l ls-bin.txt

Finally, we use the head command to display the first 10 lines of this file.

$ head ls-bin.txt
use-head-command-to-print-first-part-of-files Use head command to print first part of files
Use head command to print first part of files.

Use head command with options

First, to see all options for the head command, type the command below.

$ head --help

Next, an option that people often use with that head command is -n. This option will print number lines you selected immediately after -n.

Syntax command with -n.

$ head -n [number] [file]

For example, we will only see the first 5 lines of the ls-bin.txt file

$ head -n 5 ls-bin.txt

Use head command with the pipeline

The head command is also a command that can handle pipeline data.

Also with the /bin directory, now we do not give the result to the file but we will display the data right on the terminal with the head command.

$ ls /bin | head -n 5


This head command is quite simple, in the next article, we will learn about tail commands. An order can be considered a brother of the this command.

Continue reading the series«« Previous part: Use grep to print lines match a patternNext part: Use tail command to print last part of files »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.