Search code examples
linuxshellunixtail

How to make tail wait for files to be created before tailing them


I've been trying to find a way to make tail wait for files to be created and then start tailing them.

For example, let's assume I have a logs directory which hasn't been created yet, but my application will create it. Let's say I have another process in which I'd like to run tail -F logs/*.log and have it start tailing all the files as soon as they are created. How would I go about doing that?

And another thing: if tail is tailing some file which is deleted, is it possible to have it start tailing it again, if it's re-created?


Solution

  • With some roughness, this works:

    while inotifywait -q .; do kill $PID; tail -F -n 0 * & PID=$!; done
    

    Once anything in a directory changes, the tail command gets restarted.