41

tail -f bar/somefile.log would fail immediately when somefile.log does not exist. How do I make tail indefinitely wait for that file to be created (which will happen in a few moments)?

Update: using -F, I see:

tail: cannot open `bar/somefile.log' for reading: No such file or directory
tail: cannot watch parent directory of `bar/somefile.log': No such file or directory

because bar does't exist yet (it will be created in a few moments). when bar was created, and somefile.log was touched, tail didn't pick up the changes at all.

Rohit Gupta
  • 5,096

3 Answers3

44

You're not mentioning which OS you need it for, but tailon linux has the --retry and --follow options that will do just that;

tail --retry --follow=name somefile.log
34

This works:

while ! tail -f bar/somefile.log ; do sleep 1 ; done
cYrus
  • 22,335
10

Create the file first:

touch somefile ; tail -f somefile
psusi
  • 8,122