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

22,893FansLike
436FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Zorin OS is an Ubuntu-based Linux distro. Its ultimate goal is to provide Windows and macOS users with a Linux alternative. The Zorin OS is powerful, fast, and secure; it is pretty hard for trackers to track activities in your OS. Most users love Zorin due to its privacy prowess.
Linux Lite and Lubuntu are two very popular lightweight Linux distros designed to breathe life into decade-old hardware. Both distros are designed to run on limited system resources yet deliver a fluid user experience for regular day-to-day activities. But how well do they fare against each other?