我想在我的Mac上监视一个文件夹,然后执行一个bash脚本,将刚刚移动到或在监视目录中创建的任何文件/文件夹的名称传递给它。
当前回答
我有一个关于这个的主旨,用法很简单
watchfiles <cmd> <paths...>
为了说明,下面的命令将在每次file1或file2更改时回显Hello World;默认的间隔检查是1秒
watchfiles 'echo Hello World' /path/to/file1 /path/to/file2
如果我想每5秒检查一次,我可以使用-t标志
watchfiles -t 'echo Hello World' /path/to/file1 /path/to/file2
-v开启verbose模式,显示调试信息 -q使监视文件安静地执行(将显示#,以便用户可以看到程序正在执行) -qq使监视文件完全安静地执行 -h显示帮助和用法
https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55
其他回答
您可能想看一看(或者扩展一下)我的小工具kqwait。目前它只是坐在那里等待单个文件上的写事件,但是kqueue架构允许分层事件堆叠…
Sudo fs_usage -f filesys | grep“有趣的事情”?
下面是使用sschober工具的一行代码。
$ while true; do kqwait ./file-to-watch.js; script-to-execute.sh; done
编辑: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如何工作的示例。
你可以使用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>
要激活配置plist,请将其保存到库文件夹中的LaunchAgents文件夹“logger.plist”。
然后,您可以在shell中使用命令launchctl激活记录器。通过运行Plist:
$ launchctl load ~/Library/LaunchAgents/logger.plist
现在正在监视桌面文件夹。每次它被更改时,您都应该在system.log中看到一个输出(使用Console.app)。 要停用记录器。plist,运行:
$ launchctl unload ~/Library/LaunchAgents/logger.plist
上面的配置文件使用WatchPaths选项。或者,您也可以使用 QueueDirectories选项。有关更多信息,请参阅启动手册页。
推荐文章
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- “你有邮件”的消息在终端,os X
- Mac OS X中的环境变量
- 如何从macOS完全卸载蟒蛇
- 如何配置Mac OS X术语,使git有颜色?
- 我如何确定文件编码在OS X?
- 使iTerm以与其他操作系统相同的方式翻译“元键”
- 错误:无法在ARM处理器上的Homebrew中安装英特尔默认前缀(/usr/local)
- 在Mac OS X上哪里安装Android SDK ?
- Mac/OS X上的/var/lib/docker在哪里
- Xcode构建失败“架构x86_64未定义的符号”
- 在OSX 10.11中安装Scrapy时,“OSError: [Errno 1]操作不允许”(El Capitan)(系统完整性保护)
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 从Cocoa应用程序执行一个终端命令