tail -f, but when the file is deleted and re-created (not appended)

If your tail supports it, use tail -F, it works nicely with disappearing and re-appearing files. Just make sure you start tail from a directory which will stay in place.

-F is short-hand for --follow=name --retry: tail will follow files by name rather than file descriptor, and will retry when files are inaccessible (e.g. because they’ve been deleted).

(A number of bugs relating to --follow=name with --retry were fixed in coreutils 8.26, so you may run into issues with earlier versions; e.g. retrying when the directory containing the tailed file is deleted appears to only work in all cases with version 8.26 or later.)


Have a look at your tail man command, some have:

--follow=name

which instructs tail to watch for the name, and not the descriptor as it does by default. With such an option, if the file is deleted and recreated, tail will see it. As written in the manual:

With --follow (-f), tail defaults to following the file descriptor, which means that even if a tail'ed file is renamed, tail will continue to track its end. This default behavior is not desirable when you really want to track the actual name of the file, not the file descriptor (e.g., log rotation). Use --follow=name in that case. That causes tail to track the named file in a way that accommodates renaming, removal and creation.

Tags:

Tail