Shell 脚本监视特定文件的更改然后捕获它们
我有一系列文件,只要其中一个文件更新,我就需要对其进行“cat”。例如,假设我们有 fileA.txt、fileB.txt 和 fileC.txt。当 fileA.txt 被修改并保存文件时,我需要运行一个脚本,除了其他任务外,
cat fileA.txt fileB.txt fileC.txt > combined.txt
我还知道它涉及观看文件,但我不确定如何处理这个问题。当我处理这些文件时,监视脚本将始终运行,然后在修改一组文件中的一个文件时执行“cat”命令。我使用的是 Mac,并且希望这是用 shell 编写的。
谢谢!
I have a series of files that I need to 'cat' whenever a one of the files gets updated. For example, say we have fileA.txt, fileB.txt, and fileC.txt. When fileA.txt is modified and the file is saved I need to run a script that among other tasks will
cat fileA.txt fileB.txt fileC.txt > combined.txt
I know this deals with watching files, but am unsure how to approach this. The watching script will always be running when I'm working on these files and then execute the 'cat' command when one of the files in a set of files is modified. I'm on a Mac and would prefer if this is written in shell.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
免责声明:这可能有点矫枉过正,但另一方面可能会更快地实现并且更稳定。
有一个名为 directory_watcher 的 ruby 库,它可以监视目录的更改。
一个简单的脚本,例如
可以帮助您开始。这里修改(或删除或添加)的文件名只是打印到标准输出。
这是一个示例脚本,它将监视
file1.txt
,file2.txt
、file3.txt
。每当其中之一发生更改时,它会将它们连接到files-combined.txt
中。输出如下:
Disclaimer: This might be overkill, but on the other side maybe faster to implement and more stable.
There is a ruby library called directory_watcher, which can monitor directories for changes.
A simple script, such as
can get you started. Here the modified (or deleted or added) filenames just gets printed to stdout.
Here is an example script, that will watch for
file1.txt
,file2.txt
,file3.txt
. Whenever one of those is changed, it will concatenate them intofiles-combined.txt
.Output would be like:
您也许可以利用尾巴。
会将写入 f1.txt 和 f2.txt 的新行追加到 c.txt。为了避免 c.txt 被标题弄乱,请使用:
You might be able to utilize tail.
will append new lines that are written to either f1.txt and f2.txt to c.txt. To avoid c.txt getting cluttered with headers use: