0

This question already has an answer here:

As far as I know, tail -f <filename> allows to continuously print newly appended data from one single file.

What if I need to get content from multiple files located in multiple folders under the same parent folder, then filter that content if need be, and finally print it as a real-time stream, as new data gets appended to any one of the multiple monitored files?

EDIT: the operating system is RedHat Enterprise Linux 7.4

marked as duplicate by Stephen Kitt, Jeff Schaller, Christopher, RalfFriedl, thrig Dec 5 '18 at 0:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Did you search for multitail? – Kusalananda Dec 4 '18 at 14:13
  • @StéphaneChazelas RedHat 7.4 – KiriSakow Dec 4 '18 at 17:57
  • 1
    Are new files or directories being created/removed there while you're tailing that folder? If yes, should they be dynamically added to the list of files to monitor? May there be hard links or symlinks? – Stéphane Chazelas Dec 4 '18 at 18:01
  • @StéphaneChazelas yes indeed new folders with new files are dynamically added, and these new ones are to be monitored too. Please take a look at my own answer below and tell me what you think. – KiriSakow Dec 4 '18 at 18:07
0

Use the following oneliner:

while true; do cat /path/to/numerous/folders/and/files/*/*.txt | grep "some filter" | tail -n 10; sleep 1; done

Every 1 second, the script will print 10 last lines of the filtered stream.

To break the loop, press CtrlC.

  • Is this really what you wanted? Unlike tail -f, it will repeat the same lines over and over if there is no change, and can miss lines if more than 10 lines are added to a file in the 1-second window. – n.st Dec 4 '18 at 23:18
  • @n.st This oneliner did the job pretty well so far. At least for the time being. – KiriSakow Dec 4 '18 at 23:43
-1

Try this command:

ls /path/to/files/to/be/monitored/by/tail | while read fname; do tail -f $fname & done; wait
  • There was a typo: ls /path/to/files/to/be/monitored/by/tail | while read fname; do tail -f $fname &; done – Alireza Kazemi Dec 4 '18 at 14:30
  • 1
    Please edit your answer to fix the typo. You don't need to comment on it. – roaima Dec 4 '18 at 15:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.