Kev*_*777 5 monitoring directory tail
类似于我的上一个问题:打开一个文本文件并让它自行更新;有没有办法我可以做同样的事情,但换一个文件夹?
由于我有一个日志文件夹,我可以tail -f
与文件夹一起使用吗?
IE
$ tail -f /tmp/logs/
Run Code Online (Sandbox Code Playgroud)
我知道这行不通,但有没有其他选择?
我正在使用 RHEL 5.10
是的,有一种替代方法,经过一些研究,我发现您可以使用:
$ watch "ls -l"
Run Code Online (Sandbox Code Playgroud)
你需要在你想要的文件夹中watch
。
此外,您可以tail -10
在最后使用:
$ watch "ls -l | tail -10"
Run Code Online (Sandbox Code Playgroud)
该命令ls
每 2 秒键入一次并将输出过滤到最后 10 个文件。
如果你阅读了参考链接,它有一些很好的提示,如果你不记得上面的命令,那么你可以将以下内容添加到你的 .bashrc 文件中:
alias taildir='watch "ls -l | tail -10"'
Run Code Online (Sandbox Code Playgroud)
所以你可以直接输入taildir
而不是再次写出完整的命令。
参考:如何跟踪目录。
我不确定你到底想要什么。也许你需要inotifywait
从inotify-tools
包(在 Ubuntu 中)。不幸的是,这是仅适用于 linux 的解决方案。例如:
$ inotifywait -m -e create -e modify -r /var/log
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/var/log/ CREATE test-for-inotify1.txt
/var/log/upstart/ CREATE test-for-inotify2.txt
/var/log/ MODIFY test-for-inotify1.txt
/var/log/ MODIFY auth.log
/var/log/ MODIFY syslog
/var/log/ MODIFY auth.log
Run Code Online (Sandbox Code Playgroud)
查看man 1 inotifywait
更多观看事件和选项。
添加:
此外,如果您只需要监控某些特定文件,您可以使用tail -f
多个文件名:
$ tail -f 1.txt 2.txt 3.txt
==> 1.txt <==
==> 2.txt <==
==> 3.txt <==
==> 1.txt <==
new string in 1.txt
==> 3.txt <==
add string to 3.txt
==> 2.txt <==
And to 2.txt
^C
Run Code Online (Sandbox Code Playgroud)