How to use head and tail Command in Linux

In this post we are going to discuss – How to use head and tail Command in Linux

head

The head command is used to output a subset of lines from the file starting from the top. By default it will output the first 10 lines.

Syntax of head command

$head [OPTION]...[FILE]...

Example

$head grepfile
How to use head and tail command in Linux
Video

will display the first 10 lines of the file “grepfile”. So make sure you have more than 10 lines in your file to understand the working of head command.

how to use head/tail command in Linux

Options used with head

1. Printing Some Characters and not lines

-c: option prints first few bytes of the file. For example:

prints the first 10 characters from the file “grepfile”.

The same option can also be used to print all but the last few characters. For example suppose you want to print all the file content except for the last 10 characters.

All the contents are printed except for the last 10 characters. The result can be compared with the output of cat command which shows the entire content of the file.

2. Print first n lines

-n: prints the first n lines instead of 10. For example,

using -n option with head

prints the first two lines of the file “grepfile”

tail

The tail command is used to output a subset of lines of a file from the bottom. By default it prints the last 10 lines

Syntax

$tail [OPTION]...[FILE]...

Example

$tail grepfile

will display the last 10 lines of the file “grepfile”. So make sure you have more than 10 lines in your file to understand the working of tail command.

How to use head and tail Commands

Options Used with tail command

1. Printing Some Characters and not lines

-c: option prints last few bytes of the file. For example:

$tail -c 10 grepfile

will print the last 10 characters of the file “grepfile”

2. Print first n lines

-n: prints the last n lines instead of 10. For example,

$tail -n 5 grepfile

will print the last 5 lines of the file “grepfile”

3. To see a real time view of data being added to file

-f: This option allows you see the contents being added at real time. Open two terminals. In terminal 1 type the command

$tail -f  f1.txt

where f1.txt is a file having some content. Now you will see the last few lines of the file but the control will not come back to command prompt

How to use head and tail Commands
tail -f

Now, in second another terminal type the command

$cat>>f1.txt
write some content

You see that in the first terminal this new line “write some content” is added to the output. So, as you keep on appending content to the file it will come up in the output of tail -f command

Practice Questions on how to use head and tail command in Linux

Q1. How to display the lines starting from the 5 line?
Q2. How can you view all the contents of a file except the last 5 characters

Solutions: https://www.youtube.com/watch?v=KJCJdZeRmsU&t=9s

PPT on head and tail command

Relevant Commands

4 thoughts on “How to use head and tail Command in Linux

  1. Aw, this was a really nice post. Taking a few minutes and actual
    effort to produce a good article… but what can I
    say… I put things off a whole lot and don’t
    seem to get nearly anything done.

Leave a Reply

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