Command Line Basics: head And tail

February 12, 2011 by
Filed under: command line, HowTo, linux, Ubuntu 

Today I'm going to show you how to use the head and tail commands to view portions of text files. There are, of course, other commands that can be used to view text files. The cat command can be useful for viewing small text files while more and less can be good for larger files. But what if you're only interested in just the beginning or the end of a file? That's where head and tail come in.

As you've probably guessed by now, the head command will show the beginning of a file. By default it will show the first 10 lines of a file. Here's an example.

head /var/log/dmesg

If you'd like to view more or fewer lines you can use the -n switch followed by your desired number of lines. If you'd like to see the first 20 lines of a file you can use it like this.

head -n 20 /var/log/dmesg

Just like head will show the beginning of a file, tail will show the end of it. Here's an example.

Affiliate Link
tail /var/log/dmesg

Again, the -n switch can be used to modify the number of lines output.

tail -n 5 /var/log/dmesg

There are also some more advanced options. You can learn about them by checking out the man pages.

man head
man tail


One Response to “Command Line Basics: head And tail”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>