Linux tail from line

Using tail Command in Linux

The tail command is one of the several ways to display file contents or part of it. You can also live monitor changes made to files with it. Here are some common examples.

There are several commands to view the contents of a file in Linux. Tail command is one of them. And as the name suggests, the tail command is used to print the end part of a file. This can be useful when dealing with config files where the new changes are made to the end of the file. The tail command also allows you to view the changes made to a file live. This is extensively used for monitoring log files in real-time while troubleshooting issues. In this tutorial, I will show how you can use the tail command by sharing some practical examples of it.

How to use the tail command in Linux

To use any command in the terminal, execution of the command in the right syntax is compulsory, so let’s start with its syntax first.

  • [option] allows you to tweak the default behavior of the command.
  • [file] is where you’d enter the file name that you want to pair with the tail command.

To make things easy to understand, I will be using the 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, tail command will display all the lines by default.

But you can do more than just this! Let me tell you how.

Tail command examples

In this section, I will share different examples of the tail command, using its different options.

You’ll see some command examples with text inside <>. This indicates that you need to replace the content along with < and >with a suitable value.

1. Print the last N lines

As I mentioned earlier, the tail command prints the last ten lines by default but you may not always want the same behavior.

To print the desired number of last N lines, all you have to do is use the -n flag and append the number of the last N lines you want to be printed:

For example, here, I printed the last three lines of my text file named 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 the N line

So if you want the output from a specific point, this can be very handy.

To do so, you’d have to use the -n flag and append the line number from where you want to print the file with the + sign:

So let’s say I want to print everything from the 5th line in the text file Haruki.txt then, I will use the following command:

Print everything from the N line using 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 will be while redirecting the output of multiple files with tail commands to a single file and you want to separate the output.

To do so, you’d have to use the -v flag, which will get you a verbose output:

Here, I used the previous example, but this time, I added the option for the verbose output:

use the verbose output with the tail command

4. Use multiple files with the tail command

Sometimes, you may want to use multiple files with the tail command, and it’s pretty simple! All you have to do is use the multiple filenames and the tail command will take care of the rest:

For example, here, 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 carefully, it prints the filename by default, which is nice.

Читайте также:  Команда посмотреть все диски линукс

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

use quite mode in the tail command

5. Monitor changes made to file live

This is an excellent feature of the tail command. Not only you can see the last few lines, but it can also display any new lines added to it. How cool is that!

To do so, use the -f (follow) flag with the tail command:

Here’s an example where I used multiple windows in a single terminal. I executed the tail command in one and in the other one, I used the echo command to append the text in the file:

Sysadmins and developers use it to watch log files in real-time. It helps in troubleshooting.

The tail -F is lesser known but slightly better option. In some server configurations, the log files are rotated and created (with the same name again). The tail -f command will not track changes if the file was recreated. However, tail -F will wait for the file to be created and continue monitoring it.

Quick summary of tail command options

Here’s a brief summary covering everything I’ve mentioned so far in this tutorial:

Option Description
-n Display N lines from the end of the file
-n + Print everything from the Nth line
-v Include filename in the output header
-q Removes the filenames from the output
-f Monitor the changes made to the file in real-time
-F Monitor changes in real-time, wait for file to be (re)created

🖥️ Practice what you learn

If you want to test your recently learned knowledge of the tail command, here’s some exercise for you.

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

You can discuss the practice questions in the community:

If you are absolutely new to the command line, we have a good starting point for you.

🗨 We’ll be sharing more Linux command examples every week. Stay tuned for more. And if you have questions or suggestions, the comment section is all yours.

Источник

tail a log file from a specific line number

However, how do I make that line count a variable? Let’s say I have a large log file which is appended to daily by some program, all lines in the log file start with a datetime in this format:

Every day I want to output the tail of the log file but only for the previous days records. Let’s say this output runs just after midnight, I’m not worried about the tail spilling over into the next day. I just want to be able to work out how many rows to tail, based on the first occurrence of yesterdays date. Is that possible?

Take a look to this question, there are some interesting answer that might help you: unix.stackexchange.com/questions/47407/…

4 Answers 4

Answering the question of the title, for anyone who comes here that way, head and tail can both accept a code for how much of the file to exclude.

  • For tail, use -n +num for the line number num to start at
  • For head, use -n -num for the number of lines not to print

This is relevant to the actual question if you have remembered the number of lines from the previous time you did the command, and then used that number for tail -n +$prevlines to get the next portion of the partial log, regardless of how often the log is checked.

Answering the actual question, one way to print everything after a certain line that you can grep is to use the -A option with a ridiculous count. This may be more useful than the other answers here as you can get a number of days of results. So to get everything from yesterday and so-far today:

grep "^`date -d yesterday '+%d %b %y'`" -A1000000 log_file.txt 

You can combine 2 greps to print between 2 date ranges.

Note that this relies on the date actually occurring in the log file. It has the weakness that if no events were logged on a particular day used as the range marker, then it will fail to find anything.

To resolve that you could inject dummy records for the start and end dates and sort the file before grepping. This is probably overkill, though, and the sort may be expensive, so I won’t example it.

Источник

Оцените статью
Adblock
detector