HEAD command is print the number of lines or data of the given input. Default head command print the first 10 lines of each file. This post explains few different usage head command,

1. Default print 10 lines:

The below command print first 10 files or directories,

# ls | head

index.txt
test.txt
name.txt
30.11.2014+J.C.Bose.jpg
bc-1.06.95-921.2.x86_64.rpm
bcel-5.2-7.2.el6.x86_64.rpm
config.json
cront.txt
linuxfaq.txt
thelinux.txt


2. Print N number of Lines

Print first three files or directories,
 
# ls | grep head -3

To count listing number of lines,
 
# ls | head | wc -l
10

For example i have created a file test.txt with few lines,

3. Print All Lines from the File.

Below command print all lines from the file,
 
# head test.txt

Linux
CentOS
RedHat
Fedora
Ubuntu


4. Print First 2 Lines:

The below command print first 2 lines from the file,
 
# head -n 2 test.txt

Linux
CentOS

or
 
# head -2 test.txt

5. Print Except last N lines

The command Print all lines except last 2 lines,
 
# head -n -2 test.txt

Linux
CentOS
RedHat


6. Print the N number of bytes

Use -c option to print the N number of bytes,
 
# head -c 4 test.txt
Linu


7. Sepcific line:

If you would like to display "RedHat" line from the test.txt file,
 
# cat test.txt  | head -3 | tail -1
RedHat


Also, like to print first 3 letter "Red"
 
# cat test.txt  | head -3 | tail -1 | head -c -4
Red