是否有类似"watch"的命令?或"inotifywait"在Mac上? [英] Is there a command like "watch" or "inotifywait" on the Mac?

查看:378
本文介绍了是否有类似"watch"的命令?或"inotifywait"在Mac上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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")).

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

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.

有这样的命令吗?

推荐答案

fswatch

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

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

如果您使用的是GNU/Linux, inotifywatch ( 大多数发行版上的inotify-tools软件包都提供了类似的功能 功能.

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

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

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

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

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

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

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.

版本0.x 的较旧方法:

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

使用自制软件安装

自13年9月12日起,它已重新添加到自制软件-是!因此,更新您的公式列表(brew update),然后所需要做的就是:

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

没有自制软件的安装

Terminal.app

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

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

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.

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"的命令?或"inotifywait"在Mac上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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