如何监视新创建的文件中使用bash目录? [英] How to monitor newly created file in a directory with bash?

查看:94
本文介绍了如何监视新创建的文件中使用bash目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一堆日志文件的日志目录,一旦系统事件发生创建一个日志文件。我想写一个ONELINE bash脚本,始终监视文件列表,并在终端显示新创建的文件的内容。这里是什么样子:

目前,我只有来显示整个目录的内容:

 对于f *中;做猫$ F; DONE

它缺乏监控的功能,我想。我的系统的一个限制是,我没有 命令。我还没有安装花哨的工具的任何包管理器。原始BSD是我的一切。我确实有,我想的是像尾-F $(LS)但这尾巴的每个文件,而不是的文件列表。

在总结,我想修改我的脚本,这样我可以监控所有新创建的文件的内容。


解决方案

  1. 第一种方法 - 使用一个隐藏文件中DIR你(在我的例子它有一个名字 .watch )。然后你单行可能看起来像:

     在$ F(发现型的F -newer .watch);做猫$ F;完成的;触摸.watch


  2. 第二条本办法 - 使用inotify工具:<一href=\"http://unix.stackexchange.com/questions/273556/when-a-particular-file-arrives-then-execute-a-procedure-using-shell-script/273563#273563\">http://unix.stackexchange.com/questions/273556/when-a-particular-file-arrives-then-execute-a-procedure-using-shell-script/273563#273563


I have a log directory that consists of bunch of log files, one log file is created once an system event has happened. I want to write an oneline bash script that always monitors the file list and display the content of the newly created file on the terminal. Here is what it looks like:

Currently, all I have is to display the content of the whole directory:

for f in *; do cat $f; done

It lacks the monitoring feature that I wanted. One limitation of my system is that I do not have watch command. I also don't have any package manager to install fancy tools. Raw BSD is all I have. I do have tail, I was thinking of something like tail -F $(ls) but this tails each file instead of the file list.

In summary, I want to modify my script such that I can monitor the content of all newly created files.

解决方案

  1. First approach - use a hidden file in you dir (in my example it has a name .watch). Then you one-liner might look like:

    for f in $(find . -type f -newer .watch); do cat $f; done; touch .watch
    

  2. Second approach - use inotify-tools: http://unix.stackexchange.com/questions/273556/when-a-particular-file-arrives-then-execute-a-procedure-using-shell-script/273563#273563

这篇关于如何监视新创建的文件中使用bash目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆