Updated April 18, 2023
Introduction to Tail Command in Linux
The following article provides an outline on Tail Command in Linux. The tail command is typically used to list the last ten A in a file. It is just opposite to what HEAD command does. If more than one files are specified on the command or syntax, then it give 10 results form each of them with mentioning of file name before respectively. Now here we have prepared 3 files for demonstration through examples.
Syntax:
TAIL [option] file(s)
Option Description
Given below is the option description:
SNO | ShortCode |
Action |
1 | -n | Number of line on output. |
2 | -c | Characters bytes you want to display. |
3 | -f | Follows -mainly used while using LOGS. |
4 | -v | Verbose enable or disable. |
5 | -q | Quiet. |
Examples of Tail Command in Linux
Given below are the examples mentioned:
Example #1
Code:
tail A.txt
Output:
Explanation:
We’ve got three files here the first of all we’re going to work with is A.txt. So if we’ll just cut that out you can see that it’s the numbers 1 to 20 each on a separate line so if we just type in the tail command and then give it a file so A.txt we get the last ten A of that file displayed.
Example #2
Code:
tail -n 3 A.txt
Output:
Explanation:
We can also select the number of A that we want to display. So if we type in tail with the -n option for number of A and you can see that the same option that is used in the head command and then we type in say 3 and then the file name we get the last three A in that file.
Example #3
Code:
tail -c 4 A.txt
Output:
Explanation:
We can also use tail with the -C option to list a number of bytes. So let’s just say we want the last four bytes in that file give it the file name which is A dot txt and we’ve got the last four bytes in that file.
Example #4
Code:
tail A.txt moreA.txt
Output:
Explanation:
You can do this for multiple files so if we just type in tail and then A.txt and then more A.txt you can see that we get the last ten A in each of those files and we’ve got titles here indicating what the name of the file is.
Example #5
Code:
Java -jar test.jar >>B.txt &
Tail - f --pid 4306 B.txt
Output:
Explanation:
Now another cool option that you can use with tail is the follow option so now we are going to do first is just run this Java file so tech jar test and what this program is actually doing is it’s just adding a thousand on to the previous number and then printing it to the screen and it will continue to do this until it hits 40,000. So let me just stop that and clear the screen so we are going to run that but this time we are just sending its output into a file and we’ll call it B.txt and run that in the background.
So once we run this it will use tail to check on the progress of this currently running program. So let’s just use tail with the follow option and we are going to use another option which is –PID and enter in this process ID. We need to enter in the name of the account file and hit enter so as you can see tail started off by showing us the last ten A in that file and it’s continuing to show us A as they appear in that file so it’s following the end of the file now what’s just happened is tail has worked out that the program that was running has come to an end and it has terminated itself.
Now what actually happened was this is the process ID of this currently running program so by supplying tail with that process ID it’s able to track the state of that program and it detected that the program had finished running so therefore there’s no reason for tail to remain open and it terminated itself so that’s what the — PID option does and all it stands for is process ID okay.
Example #6
Code:
Java -jar test.jar >>B.txt &
Tail - f B.txt
Output:
Explanation:
Here we will run java again and to run that java program what is it test jar and again we are going to redirect its output and append it to a file called B.txt and run that in the background now if we type tail and use the follow option again and supply it with count what we can do is check on the progress and if we just break that what’s happened is we have stopped tail from running but if we look at the jobs that program is still running in the background and my terminal is now free again for me to do something else with it.
Conclusion
Now we have seen what the tail command does. Here we saw how -n is used to display only that number of A in output and -c to display only those number of bytes and do that quite will quietly show result and verbose is helpful while we want vary limited and clean result and show only those results which are relevant to user.
Recommended Articles
We hope that this EDUCBA information on “Tail Command in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.