The tail command in Linux is one of the most essential commands you’ll need when viewing text files. If you’re a beginner to using the command line, the tail command is a good one to learn, along with the head command, which is sort of its opposite counterpart. tail is used to print the last 10 lines of multiple files or a specified file. tail, with it’s interesting and convenient functions, can be used to monitor changes that are made to files.
What makes tail especially essential is that it allows you to use its monitoring functions for purposes such as system administration and scripting. Despite its great importance, tail is not a complex command, as with many other commonly used commands like this one.
tail can still get a tad more complex than we’ve described so far with the many different options that can be used to list the contents of a file with much more nuance and the specification included. You’ll find a few of these options very useful, although that isn’t to say that tail on its own doesn’t already cover a wide array of administration situations.
You may think that viewing files in a GUI text editor would be much easier, but this couldn’t be further from the truth. Mastering the tail command will allow you to display file contents in very specific ways for many different subtle scenarios a lot more easily and efficiently than one might think as a newcomer to Linux.
In this guide, you’ll learn how to use the tail command in Linux through examples. Follow along below to learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the tail command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The tail command prints the last x number of lines in a specific file. By default, it will display the last 10 lines of the specified files. The options you can use with tail are mostly just to display contents in a file in specific ways to meet certain needs, providing specific information that you’ll find very convenient and much more useful than simply poking around in a GUI text editor.
tail command in Linux Basic Examples
- Running the tail command by itself, without any additional options, will just print the last 10 lines of a file in your present working directory, which is just a technical way of saying the directory that your terminal is currently in.
$ tail cars.txt
As you can see in the screenshot above, the output will list a few lines per file. This output will always be the same; the same 10 lines on the “tail” end of the file, if you run tail without any additional options. You can change this result though, by using one of various options with tail. We’ll cover that in the next examples.
- Note that despite this file containing a list of 20 different car brands, simply running the tail command without any extra options prints the last 10 brands present in the file. Using this convenient little option shown below, you can get the tail command to churn out an output that’ll display a specific number (let’s say 15) of lines in a file. In our example, that’s car brands.
$ tail -n 15 cars.txt OR $ tail -15 cars.txt
As you can see in the screenshot above, the output from tail shows the last 15 lines of our file. This can be done with the
-noption or simply specifying a number after the
-sign in your command.
- An interesting but equally unique characteristic the tail command has is that it can also be written with a
+sign instead of the
-sign in our previous example. Its complementary command head does not have this. Writing the command with the
+sign and a specified number will cause the command to display data starting from the line we specified with our number.
$ tail +8 cars.txt
Our tail command has now output all the lines from our file, starting with line number 8.
- Up until now, we’ve shown you that the tail command can be used to display data of a single file. But the tail command is capable of outputting data from multiple files at once. With the
-qoption, you can do exactly that.
$ tail -q cars.txt states.txt
-qoption, the tail command was able to output the last 10 lines of both our
states.txtfile. It displays the last 10 lines by default, as we explained in an earlier section of this article.
- As we mentioned at the beginning of this article, the tail command is commonly used by system administrators to monitor the changes in log files from various programs while they’re running. The tail command is not capable of this on its own (without any options). That’s where the
-foption comes in. This option works almost exactly like tail; the only difference is that it will update when new lines are added along with the last 10 lines of a given file.
$ tail -f apache.log
You can always use the man command to read more about the tail command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The tail command is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser-known options of the tail command that we think are useful.
tail command in Linux Advanced Examples
- Use the
-voption to display data from a specific file with the name of the file preceding the data.
$ tail -v cars.txt
With this option, as you can see in the screenshot above, the
-voption for the tail command printed the last 10 lines of the
cars.txtfile with the filename displayed before it.
- The tail command can be used with pipes
|. This means that the tail command can produce an output and another command can be used in conjunction with this output for various purposes. For example, the
sortcommand with the
-roption can be used with tail command if you add a pipe after tail.
$ tail cars.txt | sort -r
In the screenshot above, you can see that the tail command printed the last 10 lines of our
cars.txtfile and the
sortcommand with the
-roption sorted those lines in the reverse order.
- Another way you can use the tail command is to display a specified number of bytes of data from a text file. For example, let’s use our
cars.txtfile and get our tail command to display the last 50 bytes of the data in the file.
$ tail -c 50 cars.txt
As you can see in the screenshot above, the last 7 car brands in our
cars.txtfile make up exactly 50 bytes of data in the file.
In this guide, we learned all about the tail command which is essential to master for users and administrators that frequently view text files on the command line. It’s especially useful for monitoring log files that have ongoing changes, by appending the