10

I'm using tail -f to print the content of a continuously changing file. When the file is truncated it shows up like this:

blah (old)..
blah more (old)..
tail: file.out: file truncated
blah..
blah more..

This can get messy when I change the file too often so that it becomes hard to see where the file begins/ends. Is there a way to somehow clear the screen when the file is truncated so that it would show up like this?

tail: file.out: file truncated
blah..
blah more..
none
  • 11,793
  • 9
  • 51
  • 87
  • Similar but not the same question: [grep - Suppress 'file truncated' messages when using tail - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/169054/suppress-file-truncated-messages-when-using-tail) – user202729 Oct 19 '21 at 15:48

3 Answers3

14

I know this is old, but another (potentially simpler) solution is:

watch -n 1 cat myfile.txt

Rhys
  • 1,439
  • 1
  • 11
  • 23
10

You could use a perl one-liner to filter the output from tail -f

e.g.

tail -f myfile.txt 2>&1 | perl -ne 'if (/file truncated/) {system 'clear'; print} else {print}'
Benj
  • 31,668
  • 17
  • 78
  • 127
  • It's trivial to see that this method will also clear the screen if the content of the file happen to have the text "file truncated". – user202729 Oct 19 '21 at 15:49
1

tailf myfile.txt

this is the command tailf rather than tail -f

with this command there is no file truncated returned on the screen

V H
  • 8,382
  • 2
  • 28
  • 48
  • this does not clean the old content from the screen as I wanted. Anyway Benj's answer is working, thanks.. – none Oct 08 '12 at 19:12
  • ye sorry my bad did see the clear initially and then whilst looking into it totally forgot typical end of day error :) glad you have it working as required – V H Oct 08 '12 at 19:18