How to use the head command in Linux

Created

head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.

By default, head will print the first 10 lines of its input to the standard output.

Print
head filename
Help
head --help
info head
man head
version information
head --version
head (GNU coreutils) 8.22

Display a Specific Number of Lines

first K lines
head -n 20 filename
Number of Bytes
head -c 100 filename
head -c 5k filename
using pipes
echo $RANDOM | sha512sum | head -c 24 ; echo
dd466c8fd2c8f9c53d35c629

The syntax for the head command is as follows:

Usage: 
  head [OPTION]... [FILE]...

OPTIONS:
-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.

Related Tags

#Linux# #head#