Tuesday, February 6, 2024

[SOLVED] clear screen when the file is truncated while using `tail -f`

Issue

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..

Solution

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}'


Answered By - Benj
Answer Checked By - David Goodson (WPSolving Volunteer)