6

I have a program that for each run generates a new log file. To watch what's going on, I'd like to tail -f the log - only problem is that I don't actually know the file name in advance.

So, is there a way to follow all files that match a certain (shell glob) pattern? I guess I could easily script something like this, but I'm wondering if there's already a solution out there.

Dave Vogt
  • 1,768

2 Answers2

5

I don't think there's a direct way to do with with tail without knowing the name - if you knew that then tail --follow=name --retry would work and it would wait for the file to appear and then start following it.

I'd suggest writing a little shell script that uses inotifywait to watch for the file appearing and then starts tail -f to follow it.

TomH
  • 3,262
1

If you don't know the file name in advance:

  1. decide on a filename, like foo.log
  2. watch that file: tail -F foo.log (doesn't matter if foo exists or not)
  3. use a tool to watch the directory for file changes and run a command

For the command, either:

  • move the new file to overwrite foo.log mv the_new_file_which_appeared foo.log (if the app logging opens once, this will work fine)
  • or just symlink: ln -s the_new_file_which_appeared foo.log - and tail should catch that.

To properly watch the directories (step 3), you need a configurable, clever tool.

Personally, I'd use Guard with the Guard::Process.

In practice, Guard is not much heavier than using shell scripts (it's a thin layer on top of inotify on Linux), and it's all very quick and easy to setup.