Using Tail Command in Linux [5 Examples]


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.

19 basic but essential Linux terminal tips you should know

Learn about some of the small, basic but often overlooked things about the device. With the little tips, you should be able to use the device a bit more efficiently.

New to the station? Learn terms

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]

here,

  • [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:

The default behavior of the tail command

πŸ’‘

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 Haruki.txt:

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

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 + Signboard:

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
Print everything from line N with the tail command

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:

Use verbose output with the tail command

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: Haruki.txt And Premchand.txt:

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

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> 
Use quiet mode at tail command

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:

Use live file monitoring in Linux with the tail command

System administrators and developers Use it to view log files in real time. Helps troubleshoot.

πŸ‹οΈ

the 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:

option a description
-n <number> Show x lines from the end of the file
-n +<number> Print everything from the ninth line
-v Include the file name in the output header
-q Removes file names from the output
-f Monitor changes made to the file in real time
-F 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.

  1. Print the last 12 lines of the file
  2. Print only the last line of the file
  3. Add the file name to the output while viewing the lines starting from line 15
  4. Print only the 11th line (combine it with the head command)

You can discuss practice questions in the community:

It is the free and open source software community

A place for desktop Linux users and FOSS readers

If you’re completely new to the command line, we’ve got a good starting point for you.

Linux command tutorials for absolute beginners

Never used Linux commands before? No thanks necessary. This tutorial series is for absolute beginners to the Linux terminal.

πŸ—¨ 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.



Source link

Leave a Comment

Your email address will not be published. Required fields are marked *