This post was originally published on go2linux.org, a Linux blog I ran from 2007 to 2011. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

Sometimes you may want to take a look at a file, only to the first lines of it, this is specially useful when you are watching at CSV files that you may be using to work with MySQL or PosgreSQL databases, in this cases what we need is to look at the head of the file to see its structure, but not all the file. To do this we use the head command.

Description

Displays the beginning of a file (as many lines as you want).

Usage

head [options] [file]

Options

  • -c, --bytes=[-]N print the first N bytes of each file; with the leading ‘-’, print all but the last N bytes of each file
  • -n, --lines=[-]N print the first N lines instead of the first 10; with the leading ‘-’, print all but the last N lines of each file
  • -q, --quiet, --silent never print headers giving file names
  • -v, --verbose always print headers giving file names

this is a really useful command, and as you can see its use is really easy.

Examples

head -3 minicom.log

Output:

20070221 17:35:11 Hangup (0:00:00)
20070221 17:52:16 Gone offline (0:00:06)
20070221 17:57:07 Gone offline (0:00:07)

Or:

head -6 minicom.log

Output:

20070221 17:35:11 Hangup (0:00:00)
20070221 17:52:16 Gone offline (0:00:06)
20070221 17:57:07 Gone offline (0:00:07)
20070221 18:44:53 Gone offline (0:00:06)
20070221 18:46:35 Gone offline (0:00:06)
20070221 21:17:20 Hangup (0:00:00)