监视一组文件的更改并在更改时对其执行命令
我想到的(命令行)界面如下所示:
watching FILE+ do COMMAND [ARGS] (and COMMAND [ARGS])*
COMMAND
中出现的任何“{}
”都将替换为更改的文件的名称。 请注意,“do
”和“and
”是关键字。
例如:
> watching foo.txt bar.txt do scp {} somewhere.com:. and echo moved {} to somewhere
或者:
> watching foo.c do gcc foo.c and ./a.out
但我并不热衷于该界面。 我将添加我的脚本来作为答案,看看是否有人有更好的东西或改进它的方法。
The (command line) interface I have in mind is like so:
watching FILE+ do COMMAND [ARGS] (and COMMAND [ARGS])*
Where any occurrence of "{}
" in COMMAND
is replaced with the name of the file that changed. And note that "do
" and "and
" are keywords.
For example:
> watching foo.txt bar.txt do scp {} somewhere.com:. and echo moved {} to somewhere
Or:
> watching foo.c do gcc foo.c and ./a.out
I'm not wedded to that interface though. I'll add my script that does that as an answer and see if anyone has anything better or ways to improve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
entr
,例如不幸的是您无法检查哪个文件已更改,但是使用 rsync -u 会自动跳过未更改的文件。
这是第二个示例:
或者简单地使用
make
,例如要监视目录的更改,请使用包含在 shell 循环内的
-d
参数,例如:You can use
entr
, e.g.Unfortunately you can't check which file has been changed, but using
rsync -u
will automatically skip files which haven't been changed.Here is the second example:
or simply use
make
, e.g.To watch the directory for changes, use
-d
parameter wrapped inside a shell loop, e.g.:请检查
inotify
工具。Please check the
inotify
tools.我刚刚发现这个用 perl 编写的
every_change
脚本,它与我在答案中发布的一个。所以基本上每次文件更改时它都会执行一些操作。
I just found this
every_change
script written in perl, that's very similar to the one I posted in my answer.So basically it does something every time a file changes.