7 ways to use the Linux Head command

In this session of Terminal Tuts, we shall learn how to use the Head command in Linux. In short, Head is used to display the required number of lines from a file.

Linux has a set of powerful commands for doing different operations. Among those commands is the Head. It is also an important command that use to display the N numbers of lines of a file. Why is it needed? Imagine a scenario where you are working on the Linux command line, and continuously a log file is being built or getting updated.

Your need is only to view the first few lines to make sure things are working as intended. That’s when the Head command will be handy as it can quickly show only the first few lines of the file.

Syntax:

head <option> <file>

Where <option> are different parameters that can be used with the head command for various purposes.

Head Command in Linux Examples

1. Display the first ten lines

By default, it returns the first ten lines of the file. If more than one filename is provided, then it returns the first ten lines of each file.

head /etc/passwd

Example:

display-first-10-lines-using-head-command
Display the first ten lines using the head command

2. Display the first N lines

Instead of displaying the first ten lines, you can control the number of lines you want to show. The general syntax for such a purpose is as follows:

head -n <num> <file>

Where <num> is a mandatory parameter that represents the number of lines you wish to show in the output.

head -n 5 /etc/passwd

Example:

display first N lines using head command

3. Display the first N bytes

You can also define the number of first bytes you want to show in the output instead of lines.

Syntax:

head -c <num> <file>

Where <num> is a mandatory parameter that represents the number of bytes you want to display in the output.

head -c 50 /etc/passwd

Example:

display first N bytes using head command
display first N bytes using head command

4. Display file data with Header

We can use the head command to display lines from multiple files always preceded by filename header.

Syntax:

head -v  <file1> <file2>
head -v /etc/passwd /etc/shadow

Example:

displaying file contents with filename header
displaying file contents with filename header

5. Display file data without Header

We can use the head command to display lines from multiple files without preceded by filename header.

Syntax:

head -q  <file1> <file2>
head -q /etc/passwd /etc/shadow

Example:

displaying file contents without filename header
displaying file contents without filename header

6. Filter file data using the grep command

You can filter data from the file by combining the grep command with the head command using a pipe.

Syntax:

head <file> | grep <searchterm>

Here <searchterm> is the string you want to search and display within the file.

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

This command will only return lines containing our search term ‘tuts’.

Example:

Filter File data using grip command
Filter File data using grip command

7. Display N most recently used files

Head command can be combined using a pipe with other Linux commands. Like you can use the head command with ls command to get N most recent used files.

Syntax:

ls -t | head -n 5

This command will first find the most recent files and will display only the top 5 most recently used files.

Example:

display most recently used files

Conclusion

As you can see, the head command is handy for manipulating large files, especially the vast log files where you want to see recent data instead of opening up a complete file, which may consume a lot of memory and time.

Habib Ahmed
He is a professional writer with more than seven years of experience in the field. He also enjoys technical writing/ manuals, white papers, and scientific presentations.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,281FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

The 6 Best Linux File Recovery Software

Now and then, most of us lose important data in our computers either through accidental deletion, virus attacks, permanent removal of files, etc. Some of these files contain critical information that cannot be assumed and needs recovery. In this post, we will discuss some of the best data recovery available for Linux systems. The fantastic part is that most of them are opensource and freely available for use.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.

Ubuntu 19.10 (Eoan Ermine) Beta Installation and Overview

It may be early, but I've been looking forward to the release of Ubuntu 19.10 for some time now. As an impatient person, and promised readers in the FOSSLinux article announcing the release of Eoan Ermine that we would provide you a review of Ubuntu 19.10 Eoan Ermine - Beta.  So, here it goes.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.