Is there a command like “watch” or “inotifywait” o

2019-01-02 19:18发布

I want to watch a folder on my Mac (Snow Leopard) and then execute a script (giving it the filename of what was just moved into a folder (as a parameter... x.sh "filename")).

I have a script all written up in bash (x.sh) that will move some files and other stuff on input $1 I just need OSX to give me the file name when new files/folders are moved/created into a dir.

Any such command?

12条回答
高级女魔头
2楼-- · 2019-01-02 19:29

This is just to mention entr as an alternative on OSX to run arbitrary commands when files change. I find it simple and useful.

查看更多
栀子花@的思念
3楼-- · 2019-01-02 19:30

Here's a one-liner using sschober's tool.

$ while true; do kqwait doc/my_file.md; make; done
查看更多
残风、尘缘若梦
4楼-- · 2019-01-02 19:31

Facebook's watchman, available via Homebrew, also looks nice. It supports also filtering:

These two lines establish a watch on a source directory and then set up a trigger named "buildme" that will run a tool named "minify-css" whenever a CSS file is changed. The tool will be passed a list of the changed filenames.

$ watchman watch ~/src

$ watchman -- trigger ~/src buildme '*.css' -- minify-css

Notice that the path must be absolute.

查看更多
ら面具成の殇う
5楼-- · 2019-01-02 19:33

You might want to take a look at (and maybe expand) my little tool kqwait. Currently it just sits around and waits for a write event on a single file, but the kqueue architecture allows for hierarchical event stacking...

查看更多
谁念西风独自凉
6楼-- · 2019-01-02 19:33

watchdog is a cross-platform python API for watching files / directories, and it has builtin "tricks" tool that allows you to trigger actions (including shell commands) when events occur (including new added file, removed file and changed file).

查看更多
旧时光的记忆
7楼-- · 2019-01-02 19:33

Here's a simple single line alternative for users who don't have the watch command who want to execute a command every 3 seconds:

while :; do your-command; sleep 3; done

It's an infinite loop that is basically the same as doing the following:

watch -n3 your-command

查看更多
登录 后发表回答