Heads or Tails?

The head command displays the first few lines at the top of a file. It can be useful when you want a quick peek at a large file, as an alternative to opening the file with a text editor. By default, head will show the first ten lines of a file, but you can also tell it how many lines to display. Here are a couple of examples:

head some.file Show first ten lines of some.file.

head -5 some.file Show first five lines of some.file.

The tail Command

The tail command displays the last few lines of a file. Like head, it can save you time, because it's a lot quicker than calling up a file with a text editor and scrolling all the way down to the bottom. By default, tail will show the last ten lines of a file, but you can also tell it how many lines to display:

tail some.file Show last ten lines of s ome.file.
tail -3 some.file Show last three lines of some.file.

Here's a practical example of how to use the tail command. Many Linux programs put diagnostic messages in the /var/syslog/messages file when they run, so this file can get pretty large after a while. To see if your most recent command issued any messages, look at the tail end of this file by entering the tail /var/syslog/messages command.

For more information on the head command, see the head manual.

For more information on the tail command, see the tail manual.

Previous Lesson: Slicing & Dicing
Next Lesson: Sorting Data



Comments - most recent first
(Please feel free to answer questions posted by others!)

E     (08 Apr 2013, 17:49)
How would I get a shell script

$./ShellScriptName filename first-line last-line

How would I make this work for example:
./ShellScriptName filename 5 10 - will output lines 5 through 10 of the file. cheers!
Sharyl03     (29 Oct 2012, 23:08)
Thanks in advance and good luck, ! :) good luck
Hassan     (14 Oct 2012, 00:21)
If you do an "ls" in /etc, what is the 100th filename from the END?
Deena1960     (05 Sep 2012, 07:03)
I found this post earlier today while in the office Very useful Sent the link to myself and will most likely bookmark when I make it home
Claretha1999     (04 Sep 2012, 19:42)
Very informative post. Thanks for taking the time to share your view with us.
satinder kaur     (18 Jan 2012, 00:49)
tail mostly used to check logs in log table
Dave     (29 Nov 2011, 05:28)
Let's say that I have a .dat file with a few entries such as CustomerID:LastName:FirstName:Phone
I am trying to use tail to grab the last line of the customerinfo.dat file for the highest CustomerID and set a varible for that CustomerID number. Basically, I need to see if customerinfo.dat exists. If the file exists, then search the file for the last customerID, add 1 to that number to start creating a new customer. If that customerinfo.dat file does not exist, then it will set the CustomerID field to 1. Any help? I am a real newb to this.
gardenman     (07 Nov 2011, 08:41)
To show the 1st 50 lines just use head -50 <filename>.This is a great site!
janki     (05 Nov 2011, 01:03)
can we display the last 50 lines by using tail command???
Pam     (20 Apr 2011, 11:51)
We just upgraded from OS 3 to OS 5. Not sure if that is my problem or not. I have a script that displays the "head" of a file. I get lots of extra characters. This is first line: backupbanner.good
This displays correctly using the more command. Using head or cat it displays backupbanner.good100666 000350 000001 00000001031 and a lot more gobbly gook.
Any suggestions?
slamsnany     (27 Mar 2011, 18:46)
mine Sony Ericsson W700i..
suraj     (26 Dec 2010, 03:11)
i want all the bash commands like head, tail etc....plz
Kannah     (03 Dec 2010, 09:01)
I've been reading your tutorial for the past few hours..
Its all short and sweet.. Thanks buddy..
Bob Rankin     (25 Aug 2010, 14:44)
@Abhilash - Thanks! I'll be waiting for you to send your updates. How soon should I expect the "full info"?
Abhilash     (25 Aug 2010, 01:40)
Thanq but u have not given full info about it
raghu     (10 Aug 2010, 07:24)
how to print 1st 10 lines using telnet command
deepak pandey     (03 May 2010, 20:52)
how many lines selected in head & tail command
Sam     (26 Apr 2010, 22:12)
There could be a empty line at the end of the file
Deepak Soni     (15 Apr 2010, 02:11)
When i using the tail command as
tail -1 "file_name". then it not show on the telnet screen. and using the command as
tail -2 "file_name". then it shows the last line of the file. why it not show the last line with tail -1 "file_name". please reply.
R.L.     (10 Apr 2010, 08:19)
Thanks a lot, this pages on Linux data manipulation helped me with my university's exam project.
Bob Rankin     (02 Apr 2010, 15:40)
Okay, let's play a mail game. You send me $1000 through mail, and then I'll guide you about command!
Gagandeep Singh     (01 Apr 2010, 23:29)
Guide me about command through mail...

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.

Popular Pages