How to use the tail command on linux

Last modified: November 8, 2020

  • File Editing
  • Linux – Files
  • head
  • tail

We’re starting a new Linux area. If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community (and getting paid for your work, of course), have a look at the “Write for Us” page. Cheers, Eugen

1. Overview

In Linux, getting a portion of text from input files is a common operation.

There are two basic and widely used command-line utilities to output some parts of the text from the input: the head command and the tail command.

In this tutorial, we will discuss the typical usages of these two commands through examples.

2. Introduction to the head and the tail Commands

Both the head and the tail commands are members of the GNU coreutils package. They are, by default, installed in all Linux distributions.

As their names imply, the head command will output the first part of the file, while the tail command will print the last part of the file. Both commands write the result to standard output.

In later sections, we’ll take a closer look at each command and learn how to use them through examples.

3. The head Command

The syntax of the head command is pretty straightforward:

Let’s prepare a file (numbers_en.txt) as the input example to understand the command better:

The file contains English words of numbers from 1 to 100. Thus, the file has 100 lines.

The head command will, by default, write the first ten lines of the input file to the standard output:

3.1. Output a Specific Number of Lines

With the -n option, we can let the head command output the first n lines instead of the default 10.

For example, if we want to have the first seven lines printed to standard out, we’d use -n 7:

If we pass the -n option together with a number following the , for example -n -x, the head command will print all lines but the last x lines of the file.

For instance, if we want to ignore the last 97 lines from the file, we’d do -n -97:

3.2. Output a Specific Number of Bytes

In addition to displaying text by line, the head command can also print the file content by byte if we pass the -c option.

The usage of the -c option is the same as the -n option except for displaying text byte-wise instead of line-wise.

Let’s see an example of displaying only the first word “one” (3 bytes) from the file:

3.3. Output Multiple Files

The head command can also handle multiple files. To see this, let’s first prepare another input file numbers_de.txt. This file is very similar to the numbers_en.txt. The only difference is that the words in the file are in German instead of English:

Now let’s output the first five lines from both files in one shot:

4. The tail Command

The syntax of using the tail command is quite straightforward, too:

The tail command will by default write the last ten lines of the input file to the standard output:

4.1. Output a Specific Number of Lines

With the -n option, we can let the tail command output the last n lines instead of the default 10.

This example shows how to get the last seven lines from the input file:

If we pass the -n option together with a number following the “+”, for example “-n +x”, the tail command will print starting with the x-th line till the end of the file.

Let’s print from 95th line till the end of the numbers_en.txt file:

4.2. Output a Specific Number of Bytes

Similar to the head command, if we pass -c x option to the tail command, it will output only the last x bytes from the input file.

Let’s get the last number 100 from the input file:

In the example above, we passed 4 instead of 3 to the -c option. This is because there is an ending linebreak in the last line, and this linebreak occupied 1 byte.

4.3. Output Multiple Files

Like the head command, the tail command can output from multiple input files, too.

Let’s have a look at an example of printing the last five lines from two input files:

4.4. Watch a File for Changes

Sometimes the input file we want to check is changing. For example, a running application may append its output to a log file.

If we execute the tail command with the -f option on the changing file, all newly added lines will be appended to standard out.

Next, let’s see a demonstration of this feature.

First, we’ll create a simple shell script Every second it appends the current timestamp to a log file (/tmp/timestamp.log), it will append to the log file ten times:

Now, let’s start watching the log file by the tail command, then we execute the script above and observe if the newly append timestamps come to tail‘s output:

How to use the tail command on linux

As we expected, all newly appended lines are printed out.

To exit the tail command, we can press Ctrl-C.

5. Use the head and the tail Together

We’ve learned that the head command can give us the first part of a file, while the tail command can output the last part of the input file. But what if we want to get some part in the middle of a file?

To solve this problem, we can combine the two simple commands.

Let’s say we want to get from the 51st to the 55th line from an input file:

We can also get the same output by combining the head command and the tail command in a different way:

6. Conclusion

The head and the tail are two simple and straightforward Linux command-line utilities.

In this article, we’ve learned some typical usages of both commands through examples.

If we use the two commands properly, we can easily get the parts of input files for further processing.

Use ‘view’ to open a text file in Vim’s read-only mode

  • Tweet
  • Share
  • Email

Most Linux distributions support a shell command called view. This command invokes the Vim text editor in read-only mode. It’s functionally the same as executing:

$vim -R

Vim is a powerful text editor with a complex syntax that’s often intimidating for people unfamiliar with it. The nano editor works just as well, but with an easier syntax.

How to use the tail command on linux

Viewing Files in Linux

Although Vim competently displays files, as invoked by view, other common Linux shell commands also present some or all of a text file.

Use the head command to show the beginning of the file. Include a switch specifying a number to show a specific number of lines from the beginning of a file. For example, to show the first five lines of magnum_opus.txt, enter:

$head -5 magnum_opus.txt

Conversely, use tail to show you the last 10 lines of a file. Run tail with the -f switch to print new lines every time a file gets written at the end of it—for example, with log files.

To view the entire contents of a file, use the less command. With this utility, use the arrow keys to go back and forth one line at a time or the space or B keys to go forward or backward by one screen. Press Q to quit the utility.