HomeLinuxUtilizing head Command in Linux

Utilizing head Command in Linux [5 Examples]


The top command is without doubt one of the some ways to view the contents of a file in Linux terminal.

However that will also be achieved by the cat command too! So why use one other command?

I perceive. But when there was no downside with how the cat command works, the pinnacle command will not even exist. So let’s take a look on the downside with cat.

By default, the cat command prints all of the textual content contained in the file. However what when you’ve got a file containing 1000 or extra phrases? Your terminal will look bloated. Is not it?

Whereas the pinnacle command can specify the variety of traces to print.

On this information, I’ll stroll you thru how you should utilize the pinnacle command with the assistance of some sensible examples and extra follow workouts to brush up your command-line abilities.

Prompt Learn 📖

19 Fundamental However Important Linux Terminal Suggestions You Should Know

Study some small, primary however usually ignored issues in regards to the terminal. With the small ideas, you must be capable of use the terminal with barely extra effectivity.

New to the terminal? Study the terminology

Methods to use the pinnacle command in Linux

To make use of any command in Linux, you’ll have to use the right syntax; else, you’ll get an error.

So let’s begin with the syntax for the pinnacle command:

head [options] [file]

Right here,

  • choices are used to tweak the default conduct of the pinnacle command
  • file is the place you give absolutely the path or filename of the file

To make issues straightforward, I can be utilizing a easy textual content file named Haruki.txt with the next content material:

Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Onerous-Boiled Wonderland and the Finish of the World (1985)
Norwegian Wooden (1987)
Dance Dance Dance (1990)
South of the Border, West of the Solar (1992)
The Wind-Up Fowl Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Darkish (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Males With out Girls (2014)
Killing Commendatore (2017)

And while you use the pinnacle command with none choices, it would print the primary ten traces of the file:

use head command in linux

As you’ll be able to see, it skipped the final 5 traces!

🚧

You’ll see some command examples with textual content inside <>. This means that you should exchange the content material with < and > with an appropriate worth.

Examples of the pinnacle command in Linux

On this part, I’ll stroll you thru some sensible examples of the pinnacle command. So let’s begin with probably the most helpful one.

1. Print solely the primary N traces

So if you wish to print first N traces, all it’s important to do is use the -n flag and append the variety of the primary N traces you need:

head -n number-of-lines Filename

So for instance I wish to print the primary 5 traces of Haruki.txt file, then you definitely kind the next:

head -n 5 Haruki.txt
Print only the first N lines using the head command

2. Print all the things besides the final N traces

So if you wish to limit the output by not together with the final N traces, all it’s important to do is use the identical -n flag however have to make use of the adverse quantity (-n):

head -n -<number-of-lines> <File>

So for instance I wish to exclude the final three traces and print all the things else from the Haruki.txt then the command appears like this:

head -n -3 Haruki.txt
exlcude last N lines and print everything else using the head command in linux

3. Show the identify of the file getting used

As you’ll be able to see, the pinnacle command will not print the filename by default, so if you wish to allow this conduct, all it’s important to do is use the -v flag for verbose output:

head -v <other-options> <File>

Sure, you should utilize a couple of choice directly!

So right here, I printed the primary seven traces of the Haruki.txt file and enabled the verbose output to show the identify of the file:

Display the name of the file being used

4. Use a number of recordsdata directly

So if you wish to use the totally different recordsdata, all it’s important to do is append them one after the other, adopted by area:

head <choices> <File1> <File2> <File3>

For instance, right here, I used two recordsdata and printed the primary 5 traces of every:

head -n 5 Haruki.txt Premchand.txt
use multiple files in head command

Should you discover, it mechanically prints the filename, particularly when coping with a number of recordsdata.

However in instances like redirecting the important output, you could wish to take away the filename. This will simply be completed utilizing the -q flag:

head -q <other-flags> <File1> <File2> <File3>
remove filenames while using multiple files with the head command

5. Print characters as per the given variety of bytes

So if you wish to print the traces primarily based on the byte measurement, you are able to do that utilizing the -c flag adopted by the byte measurement.

Bear in mind, for nearly each character, one character = 1 byte.

To take action, you should utilize the next  syntax:

head -c <byte-size> <File>

For instance, right here, I print characters price 100 bytes:

head -c 100 Haruki.txt
Print characters as per the given number of bytes

Equally, if you wish to skip the characters from the top of the file by specifying the bytes, all it’s important to do is use the adverse numbers:

head -c -<byte-size> <File>

For instance, right here, I skipped the final characters of the file price 100 bytes:

skip last N characters on the basis of the byte size using the head command

Summarizing the pinnacle command

This is the abstract of the pinnacle command with totally different choices:

Choice Description
-n <quantity> Specify what number of traces to print from the start of the file.
-n -<quantity> Print all the things besides the final N traces.
-v Print the identify of the file.
-q Take away the filename when working with a number of recordsdata.
-c <byte-size> Print characters as per the given variety of bytes.

Get higher with a easy train

To carry out the given workouts, you should utilize textual content recordsdata, and if you haven’t any, you’ll be able to use our textual content recordsdata from GitHub.

  1. Show the primary ten traces of the file
  2. Show all the things besides the final 5 traces of a file
  3. Show the primary 5 traces of a number of recordsdata
For intermediate customers:
  1. Show the primary 5 traces of a number of recordsdata, sorted alphabetically by file identify (Trace: pipe to kind command)
  2. Show the traces from 11 to 16 (Trace: mix it with the tail command)
  3. Rely the incidence of a selected phrase or character within the first 5 traces (Trace: pipe to grep with wc command)

Simply getting began with Terminal? We have now a collection for you!

Whereas the terminal appears scary, you’ll be able to all the time make the terminal look good, however what in regards to the studying curve it takes?

For brand spanking new customers, we got here up with a devoted collection which covers the essential instructions so you’ll be able to embrace the terminal:

Linux Command Tutorials for Absolute Freshmen

By no means used Linux instructions earlier than? No worries. This tutorial collection is for absolute newcomers to the Linux terminal.

Moreover, you’ll be able to focus on the follow questions talked about above in our group:

It’s FOSS Neighborhood

A spot for desktop Linux customers and It’s FOSS readers

I hope you now have a greater understanding of the pinnacle command.

🗨 We’ll be sharing extra Linux command examples each week. Keep tuned for extra. And when you’ve got questions or solutions, the remark part is all yours.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments