Tuesday, October 10, 2006

First 10 Lines and Last 10 Lines of a File

Often we need to display the first or last few lines of a logfile. Normally, we do this for debugging purposes.

Here are a few examples:

This shows the first line
# head -1 logfile
This shows first 5 lines
# head -5 logfile
This shows first 10 lines
# head logfile (default is 10 lines -- no options required)
This shows first 50 lines
# head -50 logfile
This shows first 100 lines
# head -100 logfile

You should get the picture by now

This shows last line
# tail -1 logfile
This shows last 5 lines
# tail -5 logfile
This shows last 10 lines
# tail logfile (this is the default and it shows you the last 10 lines)
This shows last 50 lines
# tail -50 logfile
This shows last 100 lines
# tail -100 logfile

If you want to continuously monitor a logfile, do the following:
# tail -f logfile

This gives you the first line
# sed q logfile
This show you the first 10 lines
# sed 10q logfile

4 comments:

Divya said...

Thanks buddy, sometimes small tips help a lot.

esofthub said...

Thanks divya...the comments/tips that transpire after the post is what's really valuable :)

John said...

It's great to have all of this in one place. Thanks!

Shail said...

Thanks this was really helpful.