Untitled Document
head Command

head, by default, prints the first 10 lines of each FILE to standard output. With more than one FILE, it precedes each set of output with a header identifying the file name. If no FILE is specified, or when FILE is specified as a dash ("-"), head reads from standard input.

Example 1 -

Run the below command and it will displays first ten lines of file emp.csv.

   head emp.csv    

Example 2 - Display the first 20 lines from a file

Run the head with argument -20 along with filename , the command will display first 20 lines from a file.
   head -20 emp.csv  

Example 3 - Display the first 10 lines from two files

Run the below command and it will display the first 10 lines from from emp.csv and emp1.csv.
   head -10 emp.csv emp1.csv

Example 4 -

We can run above example with -n (number) option . Same option can be used with single file as well , when we want to display lines on terminal .
   head -n 10 emp.csv emp1.csv
   head -n 20 emp.csv

Example 5 - Displaying Contents based on size specified to head command

Will output only the first twenty bytes (characters) of emp.csv. Newlines count as a single character, so if head prints out a newline, it will count it as a byte.

   head -c 20 emp.csv

Example 6 - Displaying Contents based on size specified to head command

Displays the first three megabytes.

   head -c 3M emp.csv

Example 7 -

Displays the first 1,000 lines of emp.csv.

   head -n 1K emp.csv

Example 8 -

If dash ( - ) is specified with the head command , it reads from standard input rather than file .The below command prints first 10 lines from emp.csv and emp1.csv and standard input .

   head emp.csv emp1.csv -

Example 9 -

We can print the multiple files having similar extension using special character * along with head command .The below command prints first five lines from each .txt files

   head -n 5 *.txt

Example 10 -

Just add -q ( quite) option to above example and see the result . head command will print output without file header before the lines of each individual file.

   head -n 5 -q *.txt
Untitled Document