连续打印文件 Linux 终端的最后一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6134230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
continuously print the last line of a file Linux termin
提问by yaegerbomb
Two questions, but only stuck on one. Feel that I need the first one so someone can help me make sense of it.
两个问题,但只停留在一个。觉得我需要第一个,以便有人可以帮助我理解它。
4) Use cat and /dev/null to create an empty file.
4) 使用 cat 和 /dev/null 创建一个空文件。
5) Start a background process that continuously prints the last line of the file created in #4..
5) 启动后台进程,连续打印#4..中创建的文件的最后一行。
So what i did for number 4 was:
所以我为 4 号做的是:
cat /dev/null > emptyfile
This created an empty file. Okay so I am happy with that. The next question however confuses me. How can I read the last line of an empty file? Better yet how do I continuously do this? Running it in the background isn't a problem. Anyone have any ideas? We haven't covered scripting yet so I don't think that plays a role. As always, thanks for the help.
这创建了一个空文件。好的,所以我很高兴。然而,下一个问题让我感到困惑。如何读取空文件的最后一行?更好的是我如何不断地做到这一点?在后台运行它不是问题。谁有想法?我们还没有介绍脚本,所以我认为这不起作用。一如既往,感谢您的帮助。
采纳答案by Kim Burgaard
Use the UNIX command "tail" with the -f option. That will continuously print out contents from the file to the terminal as it is added to the file.
使用带有 -f 选项的 UNIX 命令“tail”。当它被添加到文件时,这将不断地将文件中的内容打印到终端。
Example:
例子:
tail -f emptyfile
You can terminate the tail process by typing Ctrl + C.
您可以通过键入 Ctrl + C 来终止尾部进程。
回答by pistacchio
doesn't tail -f FILE_NAME
help?
没有tail -f FILE_NAME
帮助?
回答by Ignacio Vazquez-Abrams
tail
with watch
or a loop with a delay.
tail
withwatch
或有延迟的循环。
Also, neither cat
nor /dev/null
are required.
此外,既不需要cat
也不/dev/null
需要。
> emptyfile
tail
and watch
example:
tail
和watch
例子:
watch tail -n 1 log.txt
will always show the last line of the log file. Default interval in watch
is 2 seconds.
watch tail -n 1 log.txt
将始终显示日志文件的最后一行。默认间隔watch
为 2 秒。