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.
TAIL [option] file(s)
Given below is the option description:
|Number of line on output.
|Characters bytes you want to display.
|Follows -mainly used while using LOGS.
|Verbose enable or disable.
Examples of Tail Command in Linux
Given below are the examples mentioned:
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.
tail -n 3 A.txt
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.
tail -c 4 A.txt
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.
tail A.txt moreA.txt
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.
Java -jar test.jar >>B.txt &
Tail - f --pid 4306 B.txt
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.
Java -jar test.jar >>B.txt &
Tail - f B.txt
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.
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.
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.