head command in Linux distributions is used to print the first lines of one or more files or piped data to standard output. By default, head command prints first 10 lines.
In tutorial you will learn about head command in Linux, uses of head command with examples.
Syntax of head command
The syntax for using the head command is as follows:
head [option] [file_name]
- It takes two different parameters option and file_name.
- Both parameters option and file_name are optional.
- if the input file is not specified or if the file parameter is a hyphen (-), it reads the input from the stdin stream.
To get help related to head command, use the command head –help. It will help you find the uses and options available with head command.
Options | Description |
-c, –bytes | Print the first n bytes of each file |
-n, –lines | Print the first n lines instead of the first 10 |
-q, –quiet, –silent | Never print headers giving file names |
-v, –verbose | Always print headers giving file names |
-z, –zero-terminated | Line delimiter is NUL, not newline |
–help | Display this help |
–version | Output version information |
head --help
To get the details of output version information, use head –version command.
head --version
Here I am using two files abc.txt and 123.txt to to demonstrate the usage for the head command. The contents of both files abc.txt and 123.txt are displayed below using commands cat abc.txt and cat 123.txt.
cat abc.txt
cat 123.txt
Using head command
The simplest way to use head command is using it without option. By default, head command displays the first ten lines of the input file. You can use this command with a single file or multiple files.
head filename
Display a specific number of lines from first
By default, head command displays the first ten lines of the input file. If you wish to display a specific number of lines from the beginning instead of first 10 lines. Follow the below syntax.
head -n <number> filename
Do not forget to change the <number> to a specific number of lines from the first that you wish to display. Here I am using 7 at the palace of <number>. So. It will display first 7 lines from the first.
head -n 7 abc.txt
Alternatively, you can also follow the following syntax.
head --lines <number> filename
It will also display the first n (equal to <number>) lines instead of first 10 lines.
head --lines 7 abc.txt
Display a specific number of Bytes
Specifies the number of bytes to display.
head -c <number> filename
head -c 7 abc.txt
Alternatively, you can also follow the below syntax to display the first n bytes of file.
head --bytes <number> filename
head --bytes 7 abc.txt
Display the file name
To display the file name before the standard output, use -v (–verbose) option. Follow the following syntax to display the name of the supplied file before any data it contains:
head -v filename
Here I am using abc.txt at the palace of filename.
head -v abc.txt
Display the entire file’s content
To display eniter file contents without any limit of first 10 lines or any specific number of lines, Use -z (–zero-terminated) option. -z option is used when line delimiter is NUL. As delimiter means a character that marks the beginning or end of a unit of data. If line delimiter is NUL, it will display the entire file’s content. It will behave the same as cat filename. The syntax for displaying the entire file’s content is following:
head -z filename
Here I am using abc.txt at the palace of filename.
head -z abc.txt
Display multiple files
head command can also be used with more than one file. By default, it will display the first ten lines from each provided file (proceeded by the file name). The syntax for displaying the first ten lines from each provided file:
head filename1 filename2 .....
Here I am using abc.txt and 123.txt at the palace of filename1 and filename2. Output will display first 10 lines from each provided file (proceeded by the file name).
head abc.txt 123.txt
By default, head command with multiple files displays the name of each file before listing the first 10 lines of that file. If you wish not to display name of each file, use -q (–quiet, –silent) option. Syntax for not displaying name of each file (never print headers giving file names) is:
head -q filename1 filename2 .....
Here I am using abc.txt and 123.txt at palace of filename1 and filename2.
head -q abc.txt 123.txt
Using head with Pipeline
ls -t
ls -t | head -n 5
ls -t | head -n 5 | sort
A |
adduser | addgroup | alias | anacron | apt | aptitude | arp | at | atq | atrm | awk |
B |
basename | banner | batch | bc | bg | bzip |
C |
cat | cal | cd | chgrp | chown | cksum | chmod | clear | cmp | comm | cp |
D |
date | dd | df | diff | dir | dmidecode | du |
E |
echo | eject | env | exit | expr |
F |
factor | find | free |
G |
grep | groups | gunzip | gzip |
H |
head | history | hostname | hostnamectl | htop | hwclock | hwinfo |
I |
id | ifconfig | ionice | iostat | ip | iptables | iw | iwlist |
J |
K |
kill | kmod |
L |
last | less | ln | locate | login | lp | ls | lshw | lscpu | lsof | lsusb |
M |
man | mdsum | mkdir | more| mv |
N |
nano | nc | neofetch | netcat | netstat | nice | nmap | nproc |
O |
openssl |
P |
passwd | pidof | ping | pr | ps | pwd | pstree |
Q |
R |
rdiff-backup | reboot | rename | rm | rmdir | rnmod |
S |
scp | shred | shutdown | sleep | sort | split | ssh | stat | su | sudo | sum |
T |
tac | tail | talk | tar | tee | time | tree | top | touch | tr |
U |
unalias | uname | uniq | unzip | uptime | users |
V |
vim | vi |
W |
w | wall | watch | wc | wget | whatis | whereis | which | who | whoami |
X |
xargs |
Y |
yes | youtube-dl |
Z |
zcmp | zdiff | zip | zz |