Head command in Linux with examples
It is the complementary of Tail command. The head command, as the name implies, print the top N number of data of the given input. By default it prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.
Syntax:
head [OPTION]... [FILE]...
Let us consider two files having name state.txt and capital.txt contains all the names of the Indian states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal
Without any option it display only the first 10 lines of the file specified.
Example:
$ head state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Options
1. -n num: Prints the first ‘num’ lines instead of first 10 lines. num is mandatory to be specified in command otherwise it displays an error.
$ head -n 5 state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
2. -c num: Prints the first ‘num’ bytes from the file specified. Newline count as a single character, so if head prints out a newline, it will count it as a byte. num is mandatory to be specified in command otherwise displays an error.
$ head -c 6 state.txt
Andhra
3. -q: It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.
Without using -q option
==> state.txt capital.txt <==
Hyderabad
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar
With using -q option
$ head -q state.txt capital.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Hyderabad
Itanagar
Dispur
Patna
Raipur
Panaji
Gandhinagar
Chandigarh
Shimla
Srinagar
4. -v: By using this option, data from the specified file is always preceded by its file name.
$ head -v state.txt
==> state.txt <==
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Applications of head Command
Print line between M and N lines: For this purpose we use head, tail and pipeline(|) commands. Command is: head -M file_name | tail -(M-N), since first line takes first M lines and tail command cuts (M-N)Lines starting from end. Let say from state.txt file we have to print lines between 10 and 20.
$ head -n 20 state.txt | tail -10
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
How to use head with pipeline(|): The head command can be piped with other commands. In the following example the output of the ls command is piped to head to show only the three most recently modified files or folders.
Display all recently modified or recently used files.
$ ls -t
e.txt
d.txt
c.txt
b.txt
a.txt
Cut three most recently used file.
$ ls -t | head -n 3
e.txt
d.txt
c.txt
It can also be piped with one or more filters for additional processing. For example, the sort filter could be used to sort the three most recently used files or folders in the alphabetic order.
$ ls -t | head -n 3 | sort
c.txt
d.txt
e.txt
There are number of other filters or commands along which we use head command.
Mainly, it can be used for viewing huge log files in Unix.
What is the head command?
The head command is a command-line utility for outputting the first part of files given to it via standard input. It writes results to standard output. By default head returns the first ten lines of each file that it is given.
How to view the first ten lines of a file
To view the first ten lines of a file pass the name of a file to the head command. The first ten lines of the file will be printed to standard output.
head /usr/share/dict/words
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
How to limit the number of lines to show
To set the number of lines to show with head pass the -n option followed by the number of lines to show.
head -n 1 /usr/share/dict/words
A
How to limit the number of bytes to show
To limit the number of bytes shown with head pass the -c option. Instead of limiting by number of lines this will limit by the number of bytes passed to the -c option. In the following example the output is limited to 16 bytes.
head -c 16 /usr/share/dict/words
A
a
AA
AAA
Aache%
How to show multiple files
To show the first ten lines of multiple files pass more than one filename to the head command. This will output the first ten lines of each file to standard output with a header indicating which file is being shown.
head /usr/share/dict/words /usr/share/dict/french
==> /usr/share/dict/words <==
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
==> /usr/share/dict/french <==
ça
AAAI
abaissé
abaissa
abaissai
abaissaient
abaissais
abaissait
abaissant
abaissas
To suppress the header line pass the -q option. This can be useful to combine files.
head -q /usr/share/dict/words /usr/share/dict/french
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
ça
AAAI
abaissé
abaissa
abaissai
abaissaient
abaissais
abaissait
abaissant
abaissas
How to use head with pipes
The head command can be piped to from other commands. In the following example the output of the ls command is piped to head to only show the five most recently modified files or folders.
ls -t /etc | head -n 5
ld.so.cache
ssh
pam.d
shadow
passwd
0 Comments
Post a Comment