There are several orders to View the contents of a file in Linux. Tail command is one of them.
As the name suggests, the tail command is used to print the last part of a file. This can be useful when dealing with configuration files where new changes are made at the end of the file.
The tail command also allows you to directly view the changes made to the file. This is widely used to monitor log files in real time while troubleshooting problems.
In this tutorial, I will show how you can use the tail command by sharing some working examples of it.
How to use the tail command in Linux
To use any command in the terminal, executing the command in the correct form is mandatory, so let’s start with its syntax first.
tail [option] [file]
[option]Allows you to modify the default behavior of a command.
[file]This is where you will enter the name of the file you want to associate with the tail command.
To make things easier to understand, I’ll use a
Haruki.txt Text file containing the following line:
Hear the Wind Sing (1979) Pinball, 1973 (1980) A Wild Sheep Chase (1982) Hard-Boiled Wonderland and the End of the World (1985) Norwegian Wood (1987) Dance Dance Dance (1990) South of the Border, West of the Sun (1992) The Wind-Up Bird Chronicle (1994) Sputnik Sweetheart (1999) Kafka on the Shore (2002) After Dark (2004) 1Q84 (2009-2010) Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013) Men Without Women (2014) Killing Commendatore (2017)
And When the tail command is executed without any options, it prints the last 10 lines of the file:
Quite obviously, if there are less than 10 lines, the tail command will display all lines by default.
But you can do more than this! Let me tell you how.
Tail command examples
In this section, I will share different examples of the tail command, using its various options.
You will see some example commands with text inside <>. This indicates that you need to replace the content with <و> at an appropriate value.
1. Print the last lines of N
As I mentioned earlier, the tail command prints the last 10 lines by default but you may not always want the same behavior.
To print the required number of last N lines, all you need to do is use a file
-n Tick and append the number of the last N lines you want to print:
tail -n <Number-of-last-N-Lines> <File>
For example, here I’ve printed the last three lines of my text file called
tail -n 3 Haruki.txt
You can also use a number higher than 10 here.
2. Print everything from line N.
So if you want to output from a certain point, this can be very useful.
To do this, you must use a file
-n Mark and append the line number from where you want to print the file with extension
tail -n +<Line-number> <File>
Let’s say I want to print everything starting from the fifth line in the text file
Haruki.txt Then, I’ll use the following command:
tail -n +5 Haruki.txt
3. Print the filename with the output
You may find yourself in a situation where you want to print the filename with the output given by the tail command.
A good example would be during Redirect output of multiple files with tail commands to a single file and want to separate the output.
To do this, you must use a file
-v flag, which will give you verbose output:
tail -v [File]
Here, I used the previous example, but this time I added the verbose output option:
4. Use multiple files with the tail command
Sometimes you may want to use multiple files using the tail command, which is very simple! All you have to do is use multiple filenames and the tail command will take care of the rest:
tail <options> <File-1> <File-2> <File-n>
For example, here I’ve printed the last three lines of the two different text files:
tail -n 3 Haruki.txt Premchand.txt
And if you notice it carefully, it prints the filename by default, which is nice.
If you want to skip the file name from the outputuse the
-q Flag (silent mode):
tail -q <File-1> <File-2> <File-n>
5. Monitor changes made to the live file
This is an excellent feature of the tail command. Not only can you see the last few lines, but it can also display any new lines that have been added to it. How cool!
To do this, use a file
-f (follow) Flag of the tail command:
tail -f <File>
Here is an example where I used multiple windows in one terminal. I executed the tail command in one and in the other I used the echo command to append the text in the file:
System administrators and developers Use it to view log files in real time. Helps troubleshoot.
tail -F Less popular, but a slightly better option. On some server configurations, log files are rotated and created (again with the same name). the
tail -f The command will not keep track of changes if the file is recreated. but,
tail -F It will wait for the file to be created and continue to monitor it.
Very pleasant! is not it?
Quick summary of tail command options
Here’s a brief summary that covers everything I’ve mentioned so far in this tutorial:
||Show x lines from the end of the file|
||Print everything from the ninth line|
||Include the file name in the output header|
||Removes file names from the output|
||Monitor changes made to the file in real time|
||Monitor changes in real time, and wait for the file to (re)create|
🖥️ Practice what you learn
If you want to test your newly acquired knowledge of tail command, here are some exercises for you.
Download the agatha.txt file From our GitHub repository.
Now do the following.
- Print the last 12 lines of the file
- Print only the last line of the file
- Add the file name to the output while viewing the lines starting from line 15
- Print only the 11th line (combine it with the head command)
You can discuss practice questions in the community:
If you’re completely new to the command line, we’ve got a good starting point for you.
🗨 We will be sharing more Linux command examples every week. Stay tuned for more. And if you have questions or suggestions, the comments section is entirely yours.