简体   繁体   中英

Using tail -F to see a file changing in real-time

I have a script that collects the size of a file that is being constantly fed. I'm echoing its size to a log file (echo 'filesize is $size' > log.txt) so I only have the last size information. So, only one line.

Now, in another terminal, I wanted to tail that log file to see its size increasing in real time. It turns out, tail -f path/to/file gives me the output I want but it keeps jumping to the next line (as expected, I guess).

So, the output is something like:

$ tail -F log.txt 2>/dev/null
filesize is 1.658 GB
filesize is 1.659 GB
filesize is 1.659 GB
filesize is 1.660 GB

I wanted something more like the command "less" in which you don't have the cursor back. Maybe a better example would be "mtr", that keeps updating the information on the screen without going to next line (as opposed to traceroute).

Thank you,

使用此命令。

watch tail -n 1 log.txt
while [ 1 ]; do sleep 1; clear; tail log.txt; done

这没有传递命令和参数来watch的缺点(有时你需要跳额外的循环才能正确),并且它会清除终端。

您可以watch命令以每n秒监视文件更改/差异( -d

watch -n 5 -d cat log.txt

The fanciest solution to receive realtime information on a file ist to use inotify

Which is a linux kernel feature to receive notification when a specific file changes. You can either write your own c program which uses the functionality or you simply build a script with the inotify-wait or inotify-watch command. You probably need to install it though. But both are well documented. New versions of tail also use this linux kernel functionality

EDIT: keep in mind that this only helps you with monitoring file events. What you do when such an event occours is not my cup of tea.

PS. Have you considered that the process that writes the file MAYBE only flushes its writing buffer when a line break is present

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM