Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tail -f the latest log file with a given pattern

I work with some log system which creates a log file every hour, like follows:

SoftwareLog.2010-08-01-08
SoftwareLog.2010-08-01-09
SoftwareLog.2010-08-01-10

I'm trying to tail to follow the latest log file giving a pattern (e.g. SoftwareLog*) and I realize there's:

tail -F (tail --follow=name --retry)

but that only follow one specific name - and these have different names by date and hour. I tried something like:

tail --follow=name --retry SoftwareLog*(.om[1])  

but the wildcard statement is resoved before it gets passed to tail and doesn't re-execute everytime tail retries.

Any suggestions?

like image 444
Axiverse Avatar asked Aug 05 '10 15:08

Axiverse


People also ask

How do you tail a log file?

The tail -f command prints the last 10 lines of a text or log file, and then waits for new additions to the file to print it in real time. This allows administrators to view a log message as soon as a system creates it.

How do you tail a log file continuously?

The command tail -f will display the last 10 lines of a file, and then continuously wait for new lines, and display them as they appear. The tail command is fast and simple. But if you want more than just following a file (e.g., scrolling and searching), then less may be the command for you. Press Shift-F.

How do you make tail show the last 30 lines of a file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

How do I get the latest log file in Linux?

/var/log. This is such a crucial folder on your Linux systems. Open up a terminal window and issue the command cd /var/log. Now issue the command ls and you will see the logs housed within this directory (Figure 1).


1 Answers

I believe the simplest solution is as follows:

tail -f `ls -tr | tail -n 1`

Now, if your directory contains other log files like "SystemLog" and you only want the latest "SoftwareLog" file, then you would simply include a grep as follows:

tail -f `ls -tr | grep SoftwareLog | tail -n 1`
like image 180
devup Avatar answered Oct 13 '22 15:10

devup