The head command is used to print the first few lines of a text file. By default, the head command prints the first 10 lines of each file to standard output, which is the display screen. It can be used with other commands to provide more specific and results from the system.
In a previous article, we learned how to use the tail command and its different options. In this article, we'll learn how to use the Linux head command to read the first few lines of any text file and it's the most common options.
1) Using head with standard input
If no file is specified, or when the file is specified as -, the head command reads from the standard input stream (stdin) rather than a file. This means it throws back everything typed from your keyboard. In this example, I'll input words and then head will display what I typed. After inputting, I'll press CTRL + C to quit.
$ head -
this
this
is
is
an
an
example
example
of
of
head
head
with no file specified
with no file specified
2) Viewing the first 10 lines of a file
Specifying a filename with head prints out the first 10 lines.
$ head /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
3) Viewing the first N lines of a file
You can specify the exact number of lines to display other than the first 10. You can do this by using the -n
or --lines
switch and then the number of lines you want to print.
$ head -n11 /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
4) Displaying multiple files
If multiple files are given as the argument, it displays the first 10 lines and precedes each with a header displaying the name of the file.
$ head /etc/crontab /etc/group
==> /etc/crontab <==
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
==> /etc/group <==
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
If you don't want to print headers, you can use the -q
, --quite
, or --silent
switches.
$ head --silent /etc/crontab /etc/group
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
To always print headers, you can leave the file name as is, or use the -v
or --verbose
switch.
To display the first N lines of several files you can use the command below.
$ head -3 /etc/group /etc/fuse.conf /etc/passwd
==> /etc/group <==
root:x:0:
daemon:x:1:
bin:x:2:
==> /etc/fuse.conf <==
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)
# Set the maximum number of FUSE mounts allowed to non-root users.
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You can use the * wildcard to print lines of every file whose name ends with a specific extension.
$ head -1 -q /etc/*.conf
# /etc/adduser.conf: `adduser' configuration.
# this file sets defaults for apg if apg is called without parameters
#
# Documents/brltty.conf. Generated from brltty.conf.in by configure.
# This file lists certificates that you wish to use or to ignore to be
# This is the main config file for debconf. It tells debconf where to
# /etc/deluser.conf: `deluser' configuration.
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)
[fwupd]
# Configuration for getaddrinfo(3). ## This is the default configuration for hdparm for Debian. It is a # The "order" line is only used by old versions of the C library. # # Kernel Image management overrides # include /etc/ld.so.conf.d/*.conf default_driver=pulse # This is the configuration file for libaudit tunables.
5) Printing the last N bytes of a file
To print the last N bytes of a file, you should use the -c
or --bytes
switch followed by the number of bytes. This prints the first 50 bytes of the file. You can also use
$ head -c 70 /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
To display another size other than bytes e.g Kilobytes, you can specify kB
, MB
, M
, etc
$ head -c 2kB /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,eyramm
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
voice:x:22:
Alternatively, you can use -c +N
to output bytes starting with the Nth of each file
$ head -c +50 /etc/group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:sy
To remove the last N lines and print everything else, we use the command:
6) Using head with pipes
The head command can be piped to or from other commands. This command lists the contents of a log file, pipes it to head to display the first 5 lines with a header and then the sort command displays the results in reverse order.
$ cat /var/log/dpkg.log | head -n 5 -v | sort -r
==> standard input <==
2017-02-15 20:18:59 status unpacked base-passwd:amd64 3.5.39
2017-02-15 20:18:59 status unpacked base-passwd:amd64 3.5.39
2017-02-15 20:18:59 status half-installed base-passwd:amd64 3.5.39
2017-02-15 20:18:59 startup archives install
2017-02-15 20:18:59 install base-passwd:amd64 <none> 3.5.39
The following example prints a portion of the bootstrap.log file, i.e. from the 10th line to the 20th line. This means the -n20
switch prints the first 20 lines while the -n10
switch prints the last 10 of the initial 20 lines.
$ head -n20 /var/log/bootstrap.log | tail -n 10
dpkg: warning: parsing file '/var/lib/dpkg/status' near line 4 package 'dpkg':
missing maintainer
dpkg: warning: parsing file '/var/lib/dpkg/status' near line 4 package 'dpkg':
missing architecture
Selecting previously unselected package base-passwd.
(Reading database ... 0 files and directories currently installed.)
Preparing to unpack .../base-passwd_3.5.39_amd64.deb ...
Unpacking base-passwd (3.5.39) ...
dpkg: base-passwd: dependency problems, but configuring anyway as you requested:
base-passwd depends on libc6 (>= 2.8); however:
You should now understand how the head command and its arguments work in display the first few lines of a file. It functions in the exact opposite way of tail and it can be used with other commands as well using pipes. You can refer head command man pages for more information.