打开一个目录并使用“tail -f”让它自行更新

Kev*_*777 5 monitoring directory tail

类似于我的上一个问题:打开一个文本文件并让它自行更新;有没有办法我可以做同样的事情,但换一个文件夹?

由于我有一个日志文件夹,我可以tail -f与文件夹一起使用吗?

IE

$ tail -f /tmp/logs/
Run Code Online (Sandbox Code Playgroud)

我知道这行不通,但有没有其他选择?

我正在使用 RHEL 5.10

Kev*_*777 6

是的,有一种替代方法,经过一些研究,我发现您可以使用:

$ 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而不是再次写出完整的命令。

参考:如何跟踪目录

  • 如果与 `tail` 一起使用,我想使用 `ls -lrt` 是有意义的,所以最后几个文件总是最后更改的文件。编辑:按照您的链接后,我已经看到这正是在那里完成的。 (2认同)
  • 可能更好的主意是`watch "ls -lt | head -11"`; 这不仅会显示十个最新的文件,还会显示目录中文件的总大小。 (2认同)

loo*_*ser 5

我不确定你到底想要什么。也许你需要inotifywaitinotify-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)