How to Use the head and tail Commands for Text Processing on Linux

There are many Linux commands and tools used to process text files. But there are times when you don’t want to read the entire contents of a file, but only a specific part of it. Do you know that you can use the head and tail commands on Linux to print the beginning and end of a file, respectively?


Read on to learn how to use these two commands to effectively process and manipulate text on Linux.


What is the head command?

The head command is used to print out the beginning parts of a file. It reads the files from the beginning. If you have a file with over a thousand lines, it would be very cumbersome to open and read. You can easily print out a few lines from the top with the head command.

The Head command syntax

The basic syntax of the head command is:

head [option] [file]

There are many options available besides the Head command, some of which will be discussed later. For command line help on the head command, check the manual page by running:

man head

How to use the head command

Create a file for this example: numbers.txt. In the file, list the numbers one through 20 in words. You can use any file of your choice, but make sure it has at least 11 lines.

Using the default header command

By default, the head command prints the first 10 lines of text in a file. Try it out with the numbers.txt file by running this command in your terminal:

head numbers.txt

The first 10 lines of the file are printed out:

one
two
three
four
five
six
seven
eight
nine
ten

If the file has fewer than 10 lines, the head command prints all existing lines.

You can use the head command to print a specified number of lines instead of the default 10. To print the first three lines of the numbers.txt file, run this command:

head -n 3 numbers.txt

The result:

one
two
three

Exclude the last N lines with the head command

Just as you can print out the first lines of text, you can also decide to exclude the last N lines from printing. You can do this by using a negative number for the N parameter.

To exclude the last 15 lines of the numbers.txt file, run:

head -n -15 numbers.txt

The result:

one
two
three
four
five

The head command also has the option to print the first number of characters or bytes in a file. You can do this by using the -c Possibility. To print out the first 10 characters use:

head -c 10 numbers.txt

Output:

one
two
th

What is the tail command?

As the name suggests, the tail command prints the last few lines of a file. It reads the files from the end and outputs the end lines.

The Tail command syntax

The basic syntax of the tail command is:

tail [option] [file]

For more information about the tail command, see its manual page by typing:

 man tail

How to use the tail command

For the following examples we will use the already created numbers.txt file.

Using the standard tail command

The tail command prints the last 10 lines of a file when used without special options. For example:

tail numbers.txt

The output shows the last 10 lines as mentioned:

eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty

In a situation where you don’t want to print the last 10 lines but a specific number, you can use the -n opportunity to achieve this. Run this command to print the last four lines of the numbers.txt file:

tail -n 4 numbers.txt

The result:

seventeen
eighteen
nineteen
twenty

If you want to start from a specific line N, you can use that -n option next to a positive number to achieve this. To print the output of the numbers.txt file starting from the 17th line, run this command:

tail -n +17 numbers.txt

The result:

seventeen
eighteen
nineteen
twenty

Just like the head command, you can print the last few characters in the file using the -c Possibility. Run this command to print the last 10 characters of the numbers.txt file:

tail -c 10 numbers.txt

The result:

en
twenty

Using the Head and Tail commands with multiple files

You can also use Header and Tail to print more than one file at a time. This is the syntax for using the commands with multiple file inputs:

head [option] [file1] [file2]
tail [option] [file1] [file2]

Using the Head and Tail commands together

You can even use the head and tail in the same command with the pipe symbol. The pipe symbol redirects the output of one command as input for another.

For example, to get the sixth, seventh, and eighth rows, you can run this command:

head -n 8 numbers.txt | tail -n 3

The result:

six
seven
eight

In the one-liner mentioned above, the head command displayed the first eight lines of the file, then the output was passed to the tail command, which printed the last three lines of redirected output.

Head and Tail: Helpful text editing commands

Linux offers many commands to help you manipulate and process text files effectively, and the head and tail commands are just two of many. The most common text editing commands are grep, uniq, sort, sed, awk, etc. Each of them serves a different purpose with a unique set of features.

Aside from head and tail, there are countless other text and file manipulation commands available to Linux users.

Leave a Reply

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