Categories

Show the beginning of file, with bash on Linux or Mac

Written by Guillermo Garron .
Date: 2007-05-04 14:46:30 +0000

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)
permalink

If you enjoyed the article, please share it

twitter | facebook | google+ | linkedin | email | reddit


comments powered by Disqus

Sponsor

Bio.

My name is Guillermo Garron. I am telecommunications engineer from Bolivia

I like technology a lot, and this site is mainly dedicated to that

Social feeds

rss | twitter | email | google+

Posts via email

If you use the letter icon above and subscribe to my tinyletter newsletter, you will get one to two emails, related to this site, or not. If what you want is to get the posts of this site via email, use the form below

Enter your email address:

Delivered by FeedBurner