HomeLinuxUtilizing tail Command in Linux

Utilizing tail Command in Linux [5 Examples]


There are a number of instructions to view the contents of a file in Linux. Tail command is certainly one of them.

And because the identify suggests, the tail command is used to print the tip a part of a file. This may be helpful when coping with config recordsdata the place the brand new modifications are made to the tip of the file.

The tail command additionally means that you can view the modifications made to a file stay. That is extensively used for monitoring log recordsdata in real-time whereas troubleshooting points.

On this tutorial, I’ll present how you need to use the tail command by sharing some sensible examples of it.

19 Fundamental However Important Linux Terminal Ideas You Should Know

Be taught some small, fundamental however typically ignored issues concerning the terminal. With the small ideas, it’s best to be capable to use the terminal with barely extra effectivity.

New to the terminal? Be taught the terminology

Tips on how to use the tail command in Linux

To make use of any command within the terminal, execution of the command in the appropriate syntax is obligatory, so let’s begin with its syntax first.

tail [option] [file]

Right here,

  • [option] means that you can tweak the default habits of the command.
  • [file] is the place you’d enter the file identify that you simply need to pair with the tail command.

To make issues straightforward to grasp, I will probably be utilizing the Haruki.txt textual content file containing the next line:

Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Onerous-Boiled Wonderland and the Finish of the World (1985)
Norwegian Wooden (1987)
Dance Dance Dance (1990)
South of the Border, West of the Solar (1992)
The Wind-Up Hen Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Darkish (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Males With out Ladies (2014)
Killing Commendatore (2017)

And when the tail command is executed with none choices, it prints the final 10 traces of the file:

The default behavior of the tail command

💡

Fairly clearly, if there are lower than 10 traces, tail command will show all of the traces by default.

However you are able to do extra than simply this! Let me inform you how.

Tail command examples

On this part, I’ll share totally different examples of the tail command, utilizing its totally different choices.

🚧

You’ll see some command examples with textual content inside <>. This means that you should exchange the content material together with < and > with an acceptable worth.

1. Print the final N traces

As I discussed earlier, the tail command prints the final ten traces by default however you might not all the time need the identical habits.

To print the specified variety of final N traces, all it’s important to do is use the -n flag and append the variety of the final N traces you need to be printed:

tail -n <Quantity-of-last-N-Strains> <File>

For instance, right here, I printed the final three traces of my textual content file named Haruki.txt:

tail -n 3 Haruki.txt
print the last N lines using the tail command

You can too use a quantity greater than 10 right here.

2. Print all the pieces from the N line

So if you need the output from a selected level, this may be very helpful.

To take action, you’d have to make use of the -n flag and append the road quantity from the place you need to print the file with the + signal:

tail -n +<Line-number> <File>

So for instance I need to print all the pieces from the fifth line within the textual content file Haruki.txt then, I’ll use the next command:

tail -n +5 Haruki.txt
Print everything from the N line using the tail command

3. Print the filename with the output

You might end up in a state of affairs the place you need to print the filename with the output given by the tail command.

A superb instance will probably be whereas redirecting the output of a number of recordsdata with tail instructions to a single file and also you need to separate the output.

To take action, you’d have to make use of the -v flag, which is able to get you a verbose output:

tail -v [File]

Right here, I used the earlier instance, however this time, I added the choice for the verbose output:

use the verbose output with the tail command

4. Use a number of recordsdata with the tail command

Typically, you might need to use a number of recordsdata with the tail command, and it is fairly easy! All it’s important to do is use the a number of filenames and the tail command will deal with the remaining:

tail <choices> <File-1> <File-2> <File-n> 

For instance, right here, printed the final three traces of the 2 totally different textual content recordsdata: Haruki.txt and Premchand.txt:

tail -n 3 Haruki.txt Premchand.txt
use multiple files with the tail command

And for those who discover fastidiously, it prints the filename by default, which is good.

If you wish to skip the filename from the output, use the -q flag (quiet mode):

tail -q <File-1> <File-2> <File-n> 
use quite mode in the tail command

5. Monitor modifications made to file stay

This is a superb characteristic of the tail command. Not solely you’ll be able to see the previous couple of traces, however it may well additionally show any new traces added to it. How cool is that!

To take action, use the -f (observe) flag with the tail command:

tail -f <File>

Here is an instance the place I used a number of home windows in a single terminal. I executed the tail command in a single and within the different one, I used the echo command to append the textual content within the file:

Use live monitoring of file in linux using the tail command

Sysadmins and builders use it to observe log recordsdata in real-time. It helps in troubleshooting.

🏋️

The tail -F is lesser identified however barely higher choice. In some server configurations, the log recordsdata are rotated and created (with the identical identify once more). The tail -f command is not going to monitor modifications if the file was recreated. Nonetheless, tail -F will await the file to be created and proceed monitoring it. 

Fairly cool! Is not it?

Fast abstract of tail command choices

Here is a short abstract overlaying all the pieces I’ve talked about to date on this tutorial:

Choice Description
-n <quantity> Show N traces from the tip of the file
-n +<quantity> Print all the pieces from the Nth line
-v Embrace filename within the output header
-q Removes the filenames from the output
-f Monitor the modifications made to the file in real-time
-F Monitor modifications in real-time, await file to be (re)created

🖥️ Apply what you be taught

If you wish to check your not too long ago realized data of the tail command, this is some train for you.

Obtain the agatha.txt file from our GitHub repository.

Now do the next.

  1. Print the final 12 traces of the file
  2. Print solely the final line of the file
  3. Add the filename to output whereas displaying the traces ranging from line 15
  4. Print solely the eleventh line (mix it with the pinnacle command)

You’ll be able to focus on the follow questions locally:

It’s FOSS Neighborhood

A spot for desktop Linux customers and It’s FOSS readers

If you’re completely new to the command line, now we have place to begin for you.

Linux Command Tutorials for Absolute Novices

By no means used Linux instructions earlier than? No worries. This tutorial sequence is for absolute freshmen to the Linux terminal.

🗨 We’ll be sharing extra Linux command examples each week. Keep tuned for extra. And when you have questions or strategies, the remark part is all yours.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments