head Command in Linux with Options and Examples

What is head?

The head command prints first lines of file. By default, head command prints first 10 lines of file. The file to print is passed as an argument to these commands.

How to work with head command

  1. Print the first 10 lines by default
          Example 1:  head file_name
 See above output shows first 10 lines of file /etc/passwd.

       2. Print the first N number of lines 

The head command have a -n option. To view the first N number of lines, pass file name as argument with -n option.
           Example 2: head -n 15 file_name

        3. Print the N number of bytes

         We can use -c option to print the N number of bytes from the initial (head) part of file.
          Example 3: head -c 5 file_name

       4. Passing output of other command to Head Input

        We can pass the output of other commands to he head command via pipe as shown below.
         Example 4: ls | head

Syntax and options

head [OPTIONS]… [FILE]…

 Short Option             Long Option          Description
-c                                –bytes                     to print N bytes from each input file.
-n                                –lines                       to print N lines from each input file.
-q                                –silent, –quiet          Prevent printing of header
-v                                –verbose                 to print header information always.

Related Commands

tail: Print last lines of file
cd: Used to Change Directory
pwd: Used to see Present Working Directory

Most Reading