31

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.

30

This works:

while ! tail -f bar/somefile.log ; do sleep 1 ; done
2
  • this is what finally worked for me (for missing parent directory cases). – Sridhar Ratnakumar Feb 20 '12 at 19:48
  • Will this work for Mac? – mCeviker Aug 10 '18 at 20:45
39

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
1
  • 7
    thanks, or tail -F somefile.log. but then, tail fails for a different reason (i've updated my question) – Sridhar Ratnakumar Feb 20 '12 at 19:35
8

Create the file first:

touch somefile ; tail -f somefile
2
  • Nice elegant logical solution. – CarComp Jun 12 '19 at 19:37
  • Depending on the application, this might be the best solution. Thanks! – Alex Kaszynski Jan 17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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