The tail command in Linux is often used to view the last couple of lines or data inside a file. This command is helpful while looking inside and checking some data inside a file. It is a common command used to read or check files before using it for another purpose.
A. Description
The tail command outputs the last lines of a file. But it can also be used to view only data instead of lines easily. Along with the head command, this command can be used to show specific file lines.
B. Syntax
The tail command in Linux has a straightforward, simple syntax. It takes some options after the tail command and one or more files as arguments. The syntax of the tail command is as follows:
tail [OPTION]... [FILE]...
Note: By default, the tail command will display the last 10 lines. You can use multiple or no options and change how the tail command outputs.
C. Options
The tail command has many options that can be used to change the output or use the output for some other purposes. You can also use options to view some other information like version or help etc.
Useful Options:
- -n -NUM (Shows NUM number of lines in the command line output)
- -n NUM (Shows NUM number of lines in the command line output)
- -f, –follow (Follows any addition to the file and updates the output in the command line)
- -n +NUM (Shows all the lines after NUM number of lines)
- -q, –quiet, –silent (Doesn’t output the header line with the output)
- -v, –verbose (Outputs header file names)
- -s, –sleep-interval (sets the timer interval of update for -f option)
Note: The options are case-sensitive, so be careful about using them, to find more options, use the man tail command in the command line.
Practical Examples of “tail” Command in Linux
The tail command has many practical uses and this command is often used to look inside a file to view it or check the latest information inside the particular file. This comes in handy for log files as log files have the latest data at the end of the file and the tail command can be used to see them to understand any error or command output. Here, you will find some of the practical uses of the tail command that is frequently used by most Linux users.
Example 1: Using the “tail” command to Display the Last 10 Lines of a File
By default, the tail command in Linux takes a file as an argument and displays the last 10 lines as output. Here we will use 2 files named Country.txt and States.txt. The Country.txt file has the names of all the countries in the world alphabetically in a different line. The States.txt file has all the US States’ names present in alphabetic order in a different line.
The general syntax is also quite simple and is shown below:
tail FILE.txt
Let, you want to use the file Country.txt on your Desktop. You can use the tail command on that file by the following steps:
Steps to Follow >
❶ Open Terminal in the Home directory.
❷ Change the directory to Desktop by the following command. Use the ls command to check whether the file is present on the Desktop.
cd Desktop/
ls -l
❸ Type the following command to show the Last 10 countries in that list.
tail Country.txt
❹ Press the ENTER key.
Output >
As you can see, the names of the last 10 countries in that list are displayed in the command line.
Example 2: Showing N Lines of a File Using the “tail” Command
Suppose, you want to view the last 5 countries in a file, not the default 10 values. You can use the -n option to specify how many lines you want to see. There are 2 different ways to use this option. The general syntax is as follows.
tail -n -5 FILE.txt
tail -n 5 FILE.txt
tail -5 FILE.txt
In this case, suppose you want to view the last 5 country names in the list. To do that follow the steps below.
Steps to Follow >
❶ Open the Terminal in the Home Directory.
❷Change the directory to where the file resides. Here, I will get inside the Desktop and view the files using the following commands.
cd Desktop/
ls -l
❸ Type any of the three commands below with the -n option to view only the last 5 countries in the list.
tail -n -5 Country.txt
tail -n 5 Country.txt
tail -5 Country.txt
❹ Press the ENTER key.
Output >
You can see that both of these commands show the same result which is the last 5 countries in the list.
Example 3: Opening Multiple Files Using the “tail” Command
You can also view the last lines of multiple files using the tail command in Linux. In that case, you just need to use all the file names as arguments. The general syntax is as follows:
tail FILE1 FILE2
Let’s say you want to display the last 5 lines in both Country.txt and States.txt files. You have to follow the steps below:
Steps to Follow >
❶ Open the Terminal in the Home Directory.
❷ Get inside the Desktop or other directory where the files reside. Check the files using the ls command.
cd Desktop/
ls -l
❸ Type the following command to view the last 5 lines from both files.
tail -n -5 Country.txt States.txt
❹ Press the ENTER key.
Output >
You will find the last 5 lines of both files with the header file names.
Example 4: Displaying the Last N Bytes of Data from a File Using the “tail” Command
Sometimes you may want to display data not in lines, but in characters or bytes. In that case, you need to use a different option. Using the -c option, you can display the last N bytes of data inside the file. There are two general syntaxes of the command.
tail -c -20 FILE.txt
tail -c 20 FILE.txt
You can follow the steps below to try this out.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Change the directory to the folder where the file resides. In this case, you need to get inside the Desktop as that’s where the Country.txt file is.
cd Desktop/
ls -l
❸ Type the following command to view the 20 bytes of data from the files below.
tail -c -20 Country.txt
tail -c 20 Country.txt
❹ Press the ENTER key.
Output >
As you can see from the image below, only 20 character or bytes of data is shown.
Example 5: Using the “tail” Command to View All Lines after NUM Number Line
You can view all the lines after a certain line number using the -n +NUM option. It is quite similar to the other application of the -n option but in this case, the +NUM part is added where NUM is the number of lines from which the command displays all lines onwards. The general syntax is as follows.
tail -n +NUM FILE.txt
tail +NUM FILE.txt
Suppose, in this case, you want to use the Country.txt file to view all the lines from 170 onwards.
Steps to Follow >
❶ Open the Terminal in the Home director.
❷ Now get inside the director where you’re keeping the Country.txt file. In this case, the Country.txt file is inside the Desktop.
cd Desktop/
ls -l
❸ Now you can type the following command to display all the lines from line 170 onwards.
tail -n +170 Country.txt
tail +170 Country.txt
❹ Press the ENTER key.
Output >
Here is the list of country names from 170 onwards.
Here you will find all the lines from 170 onwards displayed in the command line just like the image below.
Example 6: Viewing Interactive Update of a File Using the “tail” Command
The tail command is often used as a medium to view log files. In this method, any change to the files is automatically shown in the command line where the tail command is running. To do this you need to use the -f or –follow option as these options follow any change to the file. The general syntax is shown below.
tail -f FILE.txt
In this option, the command will show the last 10 lines and keep running the command until CTRL + C is pressed to get out of the command. Until then any change inside the files will be shown in the Terminal. You can use the following steps to try this command as it’s quite often.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Again change the directory to the folder where your files remain. In this case, you need to get inside the Desktop. Use the following commands.
cd Desktop/
ls -l
❸ Type the following command to run this command.
tail -f Country.txt
❹ Now you can add a line in that file using another command prompt. Use the following echo command to redirect the echo command and append it to the Country.txt file.
echo "That's the End of All Countries in the World" >> Desktop/Country.txt
❺ You will find that the command line is showing the new line “That’s the End of All Countries in the World”
❻ Press the ENTER key.
❼ Press CTRL + C to close the command.
Output >
As you can see, just updating the file with a new line added that new line to the prompt.
Before Adding New Line:
Note: You’ve to press CTRL + C or close the Terminal to quit this command and start a new one.
After Adding the New Line:
Example 7: Using the “tail” Command Along with the “head” command to Display Specific Lines Inside a File
Sometimes you may want to find the lines between 5-10 or 75-100, This is possible using the combination of both head and tail commands. The general syntax of this command is as follows.
head -NUM1 FILE.txt|tail +NUM2
Here at first the first NUM1 files as taken by using the head command. Then from that list, tail +NUM2 will display all the numbers from NUM2. It’s a bit hard to understand. Let’s use an example. Follow the steps below.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Change into the directory where you kept the file. Here, you’ll get inside the Desktop. Use the following command.
cd Desktop/
ls -l
❸ Let, you want to view the lines from 5 to 10. Use the following command.
head -10 Country.txt|tail +5
Note: The head command should have a larger number than the tail command, otherwise there’ll be an error.
Here at first, you took the first 10 lines from the Country.txt. Then piped it through the tail command to show only the from line 5 onwards. So, in the end, you have lines from 5-10.
❹ Press the ENTER key.
Output >
As you can see, the command line only shows lines 5-10. We can check it from the actual file as well.
Conclusion
In this article, you’ve learned about some practical use of the tail command and how this command can be used with redirection and piping to achieve certain functions. You’ll certainly see the use of these commands in real life and they will come as immensely useful for your day-to-day and professional life. Again, I’ll suggest you not to memorize and just practice. That way, you’ll certainly understand the logic and use these options in much different professional use.
Similar Readings
- The “cat” Command in Linux [10 Practical Examples]
- The “head” Command in Linux [7 Practical Examples]
- The “less” Command in Linux [10 Practical Examples]
- The “more” Command in Linux [10 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners