head command

If you are working with large files and want to see the first few lines of it, you can use the head command. The base command will display the first ten lines of the file. The command can also take numeric options to adjust the numberof lines displayed.

Command: head
Synopsis: head [OPTION] [FILE]

Note that all mandatory arguments used by the long option command is also required by the short option.

Options:

Option Long name Description
-c –bytes=[-]N This option displays N bytes of the file. Using -N will print all but the last N bytes of the file.
-n –lines=[-]N This option displays N lines of the file. Using -N will print all but the last N lines of the file.
-q –quiet –silent Does not print out headers giving file names.
-v –verbose Always prints headers giving file names.

Examples

Below are a few examples of using the cd command. You can confirm your loction after running your cd command by using the pwd command.

head -c N – This command displays the first N bytes of the file. N is the number of bytes you want to display. Below we display the first 100 bytes of a file named test.txt.

 # head -c 100 test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pellentesque mattis porta. Ut non qua 

head -n N – This command and option will display the first N lines of the file. Replace N with the actual number of lines you want to view. This example shows how to display the first 3 lines of a file. Blank lines are included in the number displayed.

 # head -n 3 test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pellentesque mattis porta. Ut non quam lorem.   Donec bibendum dolor nec sodales molestie. Aenean ullamcorper iaculis enim eu feugiat. 

head -q – This command option combination forces the file to not display the header information.

 # head -q test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pellentesque mattis porta. Ut non quam lorem. Donec bibendum dolor nec sodales molestie. Aenean ullamcorper iaculis enim eu feugiat. Integer tincidunt nunc sit amet nulla commodo, ut tristique ipsum aliquam. Curabitur malesuada massa in tristique eleifend. Nulla non purus fermentum urna ullamcorper tempor. Nulla quis luctus sapien, non congue enim. Vestibulum aliquam bibendum ipsum, a sollicitudin nisi lacinia in. Nulla sed neque varius velit rutrum dignissim at in purus. Mauris nec fermentum metus. Sed vitae porta urna. Duis eget enim et odio consequat egestas in et massa. Curabitur vel justo posuere, consectetur nisl vel, ultrices nunc. In eget lacinia mi.  Etiam libero purus, euismod eu est a, aliquet vestibulum risus. Proin tempor, lacus eu porttitor ornare, nunc massa posuere nisl, sit amet malesuada elit lacus quis justo. Morbi auctor eu odio quis porta. Morbi lacus lectus, volutpat non pellentesque eu, gravida sed tortor. 

head -v – The opposite of the -q (quiet) option, the verbose option always forces the header information to display.

 # head -v test.txt ==> test.txt <== Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pellentesque mattis porta. Ut non quam lorem. Donec bibendum dolor nec sodales molestie. Aenean ullamcorper iaculis enim eu feugiat. Integer tincidunt nunc sit amet nulla commodo, ut tristique ipsum aliquam. Curabitur malesuada massa in tristique eleifend. Nulla non purus fermentum urna ullamcorper tempor. Nulla quis luctus sapien, non congue enim. Vestibulum aliquam bibendum ipsum, a sollicitudin nisi lacinia in. Nulla sed neque varius velit rutrum dignissim at in purus. Mauris nec fermentum metus. Sed vitae porta urna. Duis eget enim et odio consequat egestas in et massa. Curabitur vel justo posuere, consectetur nisl vel, ultrices nunc. In eget lacinia mi.  Etiam libero purus, euismod eu est a, aliquet vestibulum risus. Proin tempor, lacus eu porttitor ornare, nunc massa posuere nisl, sit amet malesuada elit lacus quis justo. Morbi auctor eu odio quis porta. Morbi lacus lectus, volutpat non pellentesque eu, gravida sed tortor. 

Leave a Reply