tail multiple files and grep the output [closed] tail multiple files and grep the output [closed] unix unix

tail multiple files and grep the output [closed]


You should have a look at multitail tool (Install using sudo apt-get install multitail)

In short, with multitail, you need to use the --mergeall flag for viewing output of all in one place

multitail --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log  | grep --line-buffered "Search this: " 

You can do the same without using grep

multitail -E "Search this: " --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log  

To view the output individually using multitail, this will give the filename as well.

multitail -E "Search this: " /var/links/proc2/id/myprocess*/Daily/myprocess*.log 


the mistake is that you give the files to the grep command and not the tail.

the tail -f needs to get the files as input. try:

tail -f  /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "

to get also the file names (however it will not be like grep output it is):

tail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered -e'^==> .* <==$' -e'Search this: '


This is an interesting question and the simple answer should be: Use the prefix switch with tail, but unfortunately this is currently not implemented in most versions of tail.

As I see it, you have two options: adapt the standard tools to the task (see Udys answer) or write your own tool with your favorite scripting/programming language.

Below is one way you could do it with the File::Tail::Multi module for perl. Note that you may need to install the module from CPAN (cpan -i File::Tail::Multi).

Save the following script e.g. mtail to your executable path and make the script executable.

#!/usr/bin/env perluse File::Tail::Multi;$| = 1;  # Enable autoflush$tail = File::Tail::Multi->new(RemoveDuplicate => 0,                               OutputPrefix    => 'f',                               Files           => \@ARGV);while(1) { $tail->read; $tail->print; sleep 2 }

Change OutputPrefix to 'p' if you prefer full path prefixes.

Run it like this:

mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "

You do not need to specify --line-buffered when grep is the last command, so this is sufficient:

mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep "Search this: "