Cyberithub

10 Best Examples of head command in Linux (RedHat/CentOS 7/8)

In this article, I will take you through 10 Best Examples of head command in Linux (RedHat/CentOS 7/8). head command is an open source tool in Linux used generally used to view the top Lines content of a file. Most of the times used along with tail command for efficiently viewing the file. This tool will be available in your system by default. You don’t have to install it separately. By default it will show the top 10 lines of a file. We will see different examples of this command in below section.

SYNTAX

head [OPTION]… [FILE]…

10 Best Examples of head command in Linux (RedHat/CentOS 7/8) 1

head command in Linux

Also Read: 17 Useful nc command examples in Linux (RedHat/CentOS 7/8)

Example 1: Check head command version

To check the head command version you need to run head --version command as shown below. As you can see from the output, current head command version is 8.22.

[root@localhost ~]# head --version
head (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

–version : output version information and exit

Example 2: Display First Line of a File

If you to display only first line of a file then you need to use below head command in Linux. As shown in below example here we are checking the contents of 1st Line of file1.txt file using head -1 file1.txt command.

[root@localhost ~]# head -1 file1.txt
This is cyberithub

Example 3: Display First 5 Lines of sshd_config using head command in Linux

If you want to display first 5 Lines of sshd_config file then you need to use head -n 5 /etc/ssh/sshd_config command as shown in the below example.

[root@localhost ~]# head -n 5 /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

-n : print the first K lines instead of the first 10

Example 4: Display First 10 bytes of sshd_config file using head command in Linux

If you want to check the first 10 bytes of sshd_config file then you need to use head -c 10 /etc/ssh/sshd_config command as shown below.

[root@localhost ~]# head -c 10 /etc/ssh/sshd_config
# $OpenBSD

-c : print the first K bytes of each file

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands.For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 5: Do not display headers 

If you want to display the contents of multiple files without displaying the headers then you need to use -q option with head command in Linux as shown below. As you can see below here we have two files file1.txt and file2.txt who contents is mentioned below.

[root@localhost ~]# cat file1.txt
This is cyberithub
Hello World
[root@localhost ~]# cat file2.txt
This is from cyberithub
Hello from cyberithub

Now we will see the contents of both files file1.txt and file2.txt without showing the headers using head -q file1.txt file2.txt command as shown below.

[root@localhost ~]# head -q file1.txt file2.txt
This is cyberithub
Hello World
This is from cyberithub
Hello from cyberithub

-q : never print headers giving file names

Example 6: Display first 5 lines of every .txt file using head command in Linux

If you want to display the first 5 lines of every .txt file in current directory then you need to use below head command.

[root@localhost ~]# head -n 5 *.txt
==> file1.txt <==
This is cyberithub
Hello World

==> file2.txt <==
This is from cyberithub
Hello from cyberithub

==> output.txt <==

Example 7: Display First 5 lines of every .txt file without showing headers

If you to display the contents of first 5 lines of every .txt file without showing their headers then you need to use below head command in Linux.

[root@localhost ~]# head -n 5 -q *.txt
This is cyberithub
Hello World
This is from cyberithub
Hello from cyberithub

Example 8: Always Print headers using head command in Linux

If you want to display the headers as well with the top 10 lines of files then you need to use -v option with head command in Linux. In this example, we are displaying the contents of file1.txt and file2.txt with its header information using -v option as shown below.

[root@localhost ~]# head -v file1.txt file2.txt
==> file1.txt <==
This is cyberithub
Hello World

==> file2.txt <==
This is from cyberithub
Hello from cyberithub

-v : always print headers giving file names

Example 9: Display Last 5 Lines of sshd_config using head command in Linux

If you want to display last 5 lines of sshd_config then you can easily display it by using head command in combination with tail command in Linux as shown below.

[root@localhost ~]# cat /etc/ssh/sshd_config | tail -5 | head
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

Example 10: Check all the head command Options 

If you want to check all the options available with head command then you need to check it by running head --help command as shown below.

[root@localhost ~]# head --help
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=[-]K print the first K bytes of each file;
with the leading '-', print all but the last
K bytes of each file
-n, --lines=[-]K print the first K lines instead of the first 10;
with the leading '-', print all but the last
K lines of each file
-q, --quiet, --silent never print headers giving file names
-v, --verbose always print headers giving file names
--help display this help and exit
--version output version information and exit

K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'head invocation'

 

 

Popular Recommendations:-

head command in Linux with Examples

How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

Leave a Comment