使用`tail -f`截断文件时清除屏幕

non*_*one 9 bash tail gnu-coreutils

我正在使用tail -f打印不断变化的文件的内容.截断文件时,它显示如下:

blah (old)..
blah more (old)..
tail: file.out: file truncated
blah..
blah more..
Run Code Online (Sandbox Code Playgroud)

当我经常更改文件时,这会变得很混乱,因此很难看到文件的开始/结束位置.clear当文件被截断时,是否有某种方法以某种方式显示屏幕,以便它显示如下?

tail: file.out: file truncated
blah..
blah more..
Run Code Online (Sandbox Code Playgroud)

Rhy*_*hys 14

我知道这是旧的,但另一个(可能更简单)的解决方案是:

watch -n 1 cat myfile.txt


Ben*_*enj 10

您可以使用perl单行来过滤输出tail -f

例如

tail -f myfile.txt 2>&1 | perl -ne 'if (/file truncated/) {system 'clear'; print} else {print}'
Run Code Online (Sandbox Code Playgroud)