我想在我的Mac上监视一个文件夹,然后执行一个bash脚本,将刚刚移动到或在监视目录中创建的任何文件/文件夹的名称传递给它。


当前回答

我最终为macOS做了这个。我相信这在很多方面都很糟糕:

#!/bin/sh
# watchAndRun
if [ $# -ne 2 ]; then
    echo "Use like this:"
    echo "   $0 filename-to-watch command-to-run"
    exit 1
fi
if which fswatch >/dev/null; then
    echo "Watching $1 and will run $2"
    while true; do fswatch --one-event $1 >/dev/null && $2; done
else
    echo "You might need to run: brew install fswatch"
fi

其他回答

编辑:fsw已合并到fswatch。在这个答案中,任何对fsw的引用现在都应该读为fswatch。

我在c++中写了一个fswatch的替代品,叫做fsw,它有几个改进:

It's a GNU Build System project which builds on any supported platform (OS X v. >= 10.6) with ./configure && make && sudo make install Multiple paths can be passed as different arguments: fsw file-0 ... file-n It dumps a detailed record with all the event information such as: Sat Feb 15 00:53:45 2014 - /path/to/file:inodeMetaMod modified isFile Its output is easy to parse so that fsw output can be piped to another process. Latency can be customised with -l, --latency. Numeric event flags can be written instead of textual ones with -n, --numeric. The time format can be customised using strftime format strings with -t, --time-format. The time can be the local time of the machine (by default) or UTC time with -u, --utc-time.

焊:

fsw托管在GitHub上,可以克隆它的存储库:

    git clone https://github.com/emcrisostomo/fsw

安装的焊:

FSW可以通过以下命令安装:

    ./configure && make && sudo make install

进一步的信息:

我还写了一篇介绍性的博客文章,您可以在其中找到几个关于fsw如何工作的示例。

我最终为macOS做了这个。我相信这在很多方面都很糟糕:

#!/bin/sh
# watchAndRun
if [ $# -ne 2 ]; then
    echo "Use like this:"
    echo "   $0 filename-to-watch command-to-run"
    exit 1
fi
if which fswatch >/dev/null; then
    echo "Watching $1 and will run $2"
    while true; do fswatch --one-event $1 >/dev/null && $2; done
else
    echo "You might need to run: brew install fswatch"
fi

如果你想使用NodeJS,你可以使用一个名为chokidar(实际上是chokidar-cli)的包来监视,然后使用rsync (Mac包含):

Rsync司令部:

$ rsync -avz——排除'some-file'——排除'some-dir'。/的/我的/目的地”

Chokidar cli(通过npm全局安装):

Chokidar \"**/*\" -c \"your-rsync-command-above\"

Apple OSX文件夹操作允许您根据对文件夹采取的操作自动化任务。

我可以全心全意地推荐使用watchexec。内置Rust和它只是工作™,无论你在哪个平台上!还有简单的CLI选项。