Mac上有"watch"或"inotifywait"这样的命令吗?

Min*_*int 268 macos watch inotify

我想在我的Mac(Snow Leopard)上观看一个文件夹,然后执行一个脚本(给它一个刚刚移入文件夹的文件名(作为参数... x.sh"filename")).

我有一个用bash(x.sh)编写的脚本,它会在输入$ 1上移动一些文件和其他东西我只需要OSX在新文件/文件夹被移动/创建到目录时给我文件名.

有这样的命令吗?

cwd*_*cwd 418

fswatch

fswatch是一个使用Mac OS X FSEvents API监视目录的小程序.当收到有关该目录的任何更改的事件时,将执行指定的shell命令/bin/bash

如果您使用的是GNU/Linux,则 inotifywatch(inotify-tools大多数发行版中的软件包的一部分 )提供类似的功能.

更新: fswatch现在可以在许多平台上使用,包括BSD,Debian和Windows.

语法/一个简单的例子

可以观看多个路径的新方法 - 适用于1.x及更高版本:

fswatch -o ~/path/to/watch | xargs -n1 -I{} ~/script/to/run/when/files/change.sh
Run Code Online (Sandbox Code Playgroud)

注意:如果不是,则输出的数字-o将被添加到xargs命令的末尾-I{}.如果您确实选择使用该号码,请将其{}放在命令的任何位置.

版本0.x的旧方法:

fswatch ~/path/to/watch ~/script/to/run/when/files/change.sh
Run Code Online (Sandbox Code Playgroud)

使用Homebrew安装

截至2013年12月9日,它已被添加回自制软件 - yay!因此,更新您的公式列表(brew update),然后您需要做的就是:

brew install fswatch
Run Code Online (Sandbox Code Playgroud)

没有Homebrew的安装

在中键入这些命令 Terminal.app

cd /tmp
git clone https://github.com/alandipert/fswatch
cd fswatch/
make
cp fswatch /usr/local/bin/fswatch
Run Code Online (Sandbox Code Playgroud)

如果您c的系统上没有编译器,则可能需要安装Xcode或Xcode命令行工具 - 两者都是免费的.但是,如果是这种情况,你应该只看看自制软件.

fswatch版本1.x的其他选项

Usage:
fswatch [OPTION] ... path ...

Options:
 -0, --print0          Use the ASCII NUL character (0) as line separator.
 -1, --one-event       Exit fsw after the first set of events is received.
 -e, --exclude=REGEX   Exclude paths matching REGEX.
 -E, --extended        Use exended regular expressions.
 -f, --format-time     Print the event time using the specified format.
 -h, --help            Show this message.
 -i, --insensitive     Use case insensitive regular expressions.
 -k, --kqueue          Use the kqueue monitor.
 -l, --latency=DOUBLE  Set the latency.
 -L, --follow-links    Follow symbolic links.
 -n, --numeric         Print a numeric event mask.
 -o, --one-per-batch   Print a single message with the number of change events.
                       in the current batch.
 -p, --poll            Use the poll monitor.
 -r, --recursive       Recurse subdirectories.
 -t, --timestamp       Print the event timestamp.
 -u, --utc-time        Print the event time as UTC time.
 -v, --verbose         Print verbose output.
 -x, --event-flags     Print the event flags.

See the man page for more information.
Run Code Online (Sandbox Code Playgroud)

  • 我在那里听到你说:`fswatch ./ | xargs -I {} cp {}〜/ Dropbox/backup/latest /` (4认同)
  • 我建议`fswatch -0 -v -o /path/to/watched/files | xargs -0 -n 1 -I {} [您的命令]`,其中“-0”表示 NULL。这个对我有用,编译较少 (2认同)

sak*_*kra 90

您可以将launchd用于此目的.Launchd可以配置为在修改文件路径时自动启动程序.

例如,以下launchd config plist将在/usr/bin/logger修改我的用户帐户的桌面文件夹时启动该程序:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>logger</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/logger</string>
        <string>path modified</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/sakra/Desktop/</string>
    </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

要激活配置plist,请将其保存到Library文件夹中的LaunchAgents文件夹,作为"logger.plist".

然后,您可以使用该命令launchctl通过运行以下命令激活logger.plist:

$ launchctl load ~/Library/LaunchAgents/logger.plist
Run Code Online (Sandbox Code Playgroud)

现在正在监视桌面文件夹.每次更改时,您都应该在system.log中看到输出(使用Console.app).要停用logger.plist,请运行:

$ launchctl unload ~/Library/LaunchAgents/logger.plist
Run Code Online (Sandbox Code Playgroud)

上面的配置文件使用该WatchPaths选项.或者,您也可以使用该 QueueDirectories选项.有关更多信息,请参见launchd手册页.

  • 有没有办法让它监视文件内容以及文件路径的变化? (2认同)
  • 我不这么认为.您可以使用[opensnoop](https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/opensnoop.1m.html)来实现此目的. (2认同)

Jak*_*olý 31

Facebook的守望者,通过Homebrew提供,也看起来不错.它还支持过滤:

这两行在源目录上建立监视,然后设置名为"buildme"的触发器,每当CSS文件发生更改时,该触发器将运行名为"minify-css"的工具.该工具将传递更改的文件名列表.

$ watchman watch ~/src

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

请注意,路径必须是绝对的.

  • 这是迄今为止最好的工具。 (3认同)
  • 这是一个非常好的小程序:文档很棒,它不会打开无限循环(这就是 fswatch 似乎所做的),并且它会记住您在重新启动后创建的手表并自动再次启动它们。 (2认同)

ssc*_*ber 21

你可能想看看(也许扩展)我的小工具kqwait.目前它只是坐在一起等待单个文件上的写事件,但kqueue架构允许分层事件堆叠......

  • `$ brew install kqwait && while true; 做kqwait doc/my_file.md; 使; done` (2认同)

gfx*_*onk 14

watchdog是一个用于监视文件/目录的跨平台python API,它内置了"技巧"工具,允许您在事件发生时触发操作(包括shell命令)(包括新添加的文件,删除的文件和更改的文件).


Don*_*ton 7

这仅仅是提ENTR作为OSX的替代文件时更改为运行任意命令.我发现它简单而有用.


Jos*_*ook 6

这是一个使用sschober's tool 的单线

$ while true; do kqwait ./file-to-watch.js; script-to-execute.sh; done
Run Code Online (Sandbox Code Playgroud)