有没有像“watch"这样的命令?或“通知等待"在 Mac 上? [英] Is there a command like "watch" or "inotifywait" on the Mac?

查看:39
本文介绍了有没有像“watch"这样的命令?或“通知等待"在 Mac 上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

任何这样的命令?

解决方案

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

<块引用>

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

版本 0.x 的旧方法:

fswatch ~/path/to/watch ~/script/to/run/when/files/change.sh

使用 Homebrew 安装

截至 2013 年 9 月 12 日,它已重新添加到 homebrew - 是的!因此,更新您的公式列表 (brew update),然后您需要做的就是:

brew install fswatch

不使用 Homebrew 安装

Terminal.app

中输入这些命令

cd/tmpgit 克隆 https://github.com/alandipert/fswatchcd fswatch/制作cp fswatch/usr/local/bin/fswatch

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

fswatch 1.x 版的附加选项

用法:fswatch [选项] ... 路径 ...选项:-0, --print0 使用 ASCII NUL 字符 (0) 作为行分隔符.-1, --one-event 在收到第一组事件后退出 fsw.-e, --exclude=REGEX 排除匹配 REGEX 的路径.-E, --extended 使用扩展的正则表达式.-f, --format-time 使用指定格式打印事件时间.-h, --help 显示此消息.-i, --insensitive 使用不区分大小写的正则表达式.-k, --kqueue 使用 kqueue 监视器.-l, --latency=DOUBLE 设置延迟.-L, --follow-links 跟随符号链接.-n, --numeric 打印数字事件掩码.-o, --one-per-batch 打印带有更改事件数量的单个消息.在当前批次中.-p, --poll 使用轮询监视器.-r, --recursive 递归子目录.-t, --timestamp 打印事件时间戳.-u, --utc-time 将事件时间打印为 UTC 时间.-v, --verbose 打印详细输出.-x, --event-flags 打印事件标志.有关更多信息,请参阅手册页.

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?

解决方案

fswatch

fswatch is a small program using the Mac OS X FSEvents API to monitor a directory. When an event about any change to that directory is received, the specified shell command is executed by /bin/bash

If you're on GNU/Linux, inotifywatch (part of the inotify-tools package on most distributions) provides similar functionality.

Update: fswatch can now be used across many platforms including BSD, Debian, and Windows.

Syntax / A Simple Example

The new way that can watch multiple paths - for versions 1.x and higher:

fswatch -o ~/path/to/watch | xargs -n1 -I{} ~/script/to/run/when/files/change.sh

Note: The number output by -o will get added to the end of the xargs command if not for the -I{}. If you do choose to use that number, place {} anywhere in your command.

The older way for versions 0.x:

fswatch ~/path/to/watch ~/script/to/run/when/files/change.sh

Installation with Homebrew

As of 9/12/13 it was added back in to homebrew - yay! So, update your formula list (brew update) and then all you need to do is:

brew install fswatch

Installation without Homebrew

Type these commands in Terminal.app

cd /tmp
git clone https://github.com/alandipert/fswatch
cd fswatch/
make
cp fswatch /usr/local/bin/fswatch

If you don't have a c compiler on your system you may need to install Xcode or Xcode command line tools - both free. However, if that is the case, you should probably just check out homebrew.

Additional Options for fswatch version 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.

这篇关于有没有像“watch"这样的命令?或“通知等待"在 Mac 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆