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")).
我想在Mac(雪豹)上查看一个文件夹,然后执行一个脚本(给它一个刚刚移动到文件夹的文件名(作为参数……)x。上海“文件名”))。
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.
我有一个在bash (x.sh)中编写的脚本,它将在input $1中移动一些文件和其他东西。
Any such command?
任何这样的命令呢?
12 个解决方案
#1
339
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
fswatch是一个使用Mac OS X FSEvents API监控目录的小程序。当收到关于该目录的任何更改的事件时,指定的shell命令由/bin/bash执行
If you're on GNU/Linux, inotifywatch (part of the inotify-tools
package on most distributions) provides similar functionality.
如果您使用GNU/Linux, inotifywatch(大多数发行版的inotify-tools包的一部分)提供了类似的功能。
Update: fswatch
can now be used across many platforms including BSD, Debian, and Windows.
更新:fswatch现在可以在许多平台上使用,包括BSD、Debian和Windows。
Syntax / A Simple Example
The new way that can watch multiple paths - for versions 1.x and higher:
可以监视多个路径的新方法——用于版本1。x和更高:
fswatch -o ~/path/to/watch | xargs -n1 ~/script/to/run/when/files/change.sh
The older way for versions 0.x:
老版本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:
在9/12/13的时候,它被添加到homebrew - yay!所以,更新你的公式列表(brew update),然后你需要做的就是:
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.
如果您的系统上没有c编译器,您可能需要安装Xcode或Xcode命令行工具——都是免费的。然而,如果是这样的话,你应该去看看自制啤酒。
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.
#2
83
You can use launchd for that purpose. Launchd can be configured to automatically launch a program when a file path is modified.
您可以为此目的使用launchd。Launchd可以配置为在修改文件路径时自动启动程序。
For example the following launchd config plist will launch the program /usr/bin/logger
when the desktop folder of my user account is modified:
例如,当我的用户帐户的桌面文件夹被修改时,下面的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>
To activate the config plist save it to the LaunchAgents folder in your Library folder as "logger.plist".
要激活配置plist,请将其保存到库文件夹中的LaunchAgents文件夹中,并将其保存为“logger.plist”。
From the shell you can then use the command launchctl
to activate the logger.plist by running:
然后,您可以从shell中使用命令launchctl来激活日志记录器。plist通过运行:
$ launchctl load ~/Library/LaunchAgents/logger.plist
The desktop folder is now being monitored. Every time it is changed you should see an output in the system.log (use Console.app). To deactivate the logger.plist, run:
桌面文件夹现在正在被监视。每次更改时,您应该在系统中看到一个输出。日志(用Console.app)。禁用日志记录器。plist,运行:
$ launchctl unload ~/Library/LaunchAgents/logger.plist
The configuration file above uses the WatchPaths
option. Alternatively you can also use the QueueDirectories
option. See the launchd man page for more information.
上面的配置文件使用watchpath选项。或者,您也可以使用queuedirectory选项。有关更多信息,请参见launchd手册页面。
#3
#4
19
Facebook's watchman, available via Homebrew, also looks nice. It supports also filtering:
Facebook的watchman也可以通过Homebrew应用,看起来也不错。它还支持过滤:
These two lines establish a watch on a source directory and then set up a trigger named "buildme" that will run a tool named "minify-css" whenever a CSS file is changed. The tool will be passed a list of the changed filenames.
这两行在源目录上建立一个监视,然后设置一个名为“buildme”的触发器,每当CSS文件发生更改时,该触发器将运行一个名为“minify-css”的工具。该工具将被传递一个更改文件名的列表。
$ watchman watch ~/src
美元守望看~ / src
$ watchman -- trigger ~/src buildme '*.css' -- minify-css
$ watchman -触发器~/src build dme '*。css——minify-css
Notice that the path must be absolute.
注意路径必须是绝对的。
#5
13
watchdog is a cross-platform python API for watching files / directories, and it has builtin "tricks" tool that allows you to trigger actions (including shell commands) when events occur (including new added file, removed file and changed file).
watchdog是一个用于监视文件/目录的跨平台python API,具有内置的“技巧”工具,允许在发生事件(包括新添加的文件、删除的文件和更改的文件)时触发操作(包括shell命令)。
#6
4
This is just to mention entr as an alternative on OSX to run arbitrary commands when files change. I find it simple and useful.
这仅仅是在OSX上作为替代,在文件更改时运行任意命令。我发现它既简单又有用。
#7
3
Apple OSX Folder Actions allow you to automate tasks based on actions taken on a folder.
Apple OSX文件夹操作允许您根据文件夹上的操作自动执行任务。
#8
3
Edit: fsw
has been merged into fswatch
. In this answer, any reference to fsw
should now read fswatch
.
编辑:fsw已合并为fswatch。在这个答案中,对fsw的任何引用现在都应该读fswatch。
I wrote an fswatch
replacement in C++ called fsw
which features several improvements:
我在c++中写了一个fswatch的替代品,叫做fsw,它有几个改进:
-
It's a GNU Build System project which builds on any supported platform (OS X v. >= 10.6) with
它是一个GNU构建系统项目,构建在任何支持的平台上(OS X v. >= 10.6)
./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.它的输出易于解析,因此fsw输出可以通过管道传输到另一个进程。
- Latency can be customised with
-l, --latency
. - 延迟可以定制为-l,即延迟。
- Numeric event flags can be written instead of textual ones with
-n, --numeric
. - 数字事件标志可以用-n表示,而不是文本。
- The time format can be customised using
strftime
format strings with-t, --time-format
. - 时间格式可以使用strftime格式字符串使用-t, -时间格式来定制。
- The time can be the local time of the machine (by default) or UTC time with
-u, --utc-time
. - 时间可以是机器的本地时间(默认情况下)或带有-u、- UTC时间的UTC时间。
Getting fsw:
焊:
fsw
is hosted on GitHub and can be obtained cloning its repository:
fsw托管在GitHub上,可以获得其库的克隆:
git clone https://github.com/emcrisostomo/fsw
Installing fsw:
安装的焊:
fsw
can be installed using the following commands:
可以使用以下命令安装fsw:
./configure && make && sudo make install
Further information:
进一步的信息:
I also wrote an introductory blog post where you can find a couple of examples about how fsw
works.
我还写了一篇介绍性的博客文章,在这里你可以找到一些关于fsw如何工作的例子。
#9
2
My fork of fswatch provides the functionality of inotifywait -m
with slightly less (no wait, more! I have a lot more troubles on Linux with inotifywait
...) parse-friendly output.
我的fswatch的fork提供了inotifywait -m的功能,其功能稍微少了一点(没有等待,更多了!)我在Linux上遇到了更多的麻烦,比如inotifywait…)
It is an improvement upon the original fswatch
because it sends out the actual path of the changed file over STDOUT rather than requiring you to provide a program that it forks.
这是对原始fswatch的改进,因为它将修改后的文件的实际路径发送到STDOUT上,而不是要求您提供一个它派生的程序。
It's been rock solid as the foundation of a series of scary bash scripts I use to automate stuff.
它是我用来实现自动化的一系列可怕的bash脚本的基础。
(this is off-topic) inotifywait
on Linux, on the other hand, requires a lot of kludges on top of it and I still haven't figured out a good way to manage it, though I think something based on node.js
might be the ticket.
另一方面,Linux上的inotifywait需要大量的硬件,而且我还没有想出一个好的方法来管理它,尽管我认为这是基于node的。可能是js。
#10
#11
1
I have a GIST for this and the usage is pretty simple
我对此有个大致的了解,使用起来也很简单
watchfiles <cmd> <paths...>
To illustrate, the following command will echo Hello World
every time that file1
OR file2
change; and the default interval check is 1 second
举例来说,以下命令将在文件1或文件2每次更改时回显Hello World;默认的间隔检查是1秒
watchfiles 'echo Hello World' /path/to/file1 /path/to/file2
If I want to check every 5 seconds I can use the -t
flag
如果我想每5秒检查一次,我可以使用-t标志
watchfiles -t 'echo Hello World' /path/to/file1 /path/to/file2
-
-v
enables theverbose
mode which shows debug information - -v允许显示调试信息的详细模式
-
-q
makeswatchfiles
execute quietly (#
will be shown so the user can see the program is executing) - -q使watchfiles安静地执行(#将显示,以便用户看到程序正在执行)
-
-qq
makeswatchfiles
execute completely quietly - -qq让watchfiles完全安静地执行
-
-h
shows the help and usage - -h表示帮助和用法
https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55
https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55
#12
-1
Here's a simple single line alternative for users who don't have the watch
command who want to execute a command every 3 seconds:
以下是一个简单的单行选项,供没有watch命令的用户使用,他们希望每3秒执行一次命令:
while :; do your-command; sleep 3; done
而:;做您的命令;睡眠3;完成
It's an infinite loop that is basically the same as doing the following:
它是一个无限大的循环,基本上和下面做的一样:
watch -n3 your-command
看n3命令
#1
339
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
fswatch是一个使用Mac OS X FSEvents API监控目录的小程序。当收到关于该目录的任何更改的事件时,指定的shell命令由/bin/bash执行
If you're on GNU/Linux, inotifywatch (part of the inotify-tools
package on most distributions) provides similar functionality.
如果您使用GNU/Linux, inotifywatch(大多数发行版的inotify-tools包的一部分)提供了类似的功能。
Update: fswatch
can now be used across many platforms including BSD, Debian, and Windows.
更新:fswatch现在可以在许多平台上使用,包括BSD、Debian和Windows。
Syntax / A Simple Example
The new way that can watch multiple paths - for versions 1.x and higher:
可以监视多个路径的新方法——用于版本1。x和更高:
fswatch -o ~/path/to/watch | xargs -n1 ~/script/to/run/when/files/change.sh
The older way for versions 0.x:
老版本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:
在9/12/13的时候,它被添加到homebrew - yay!所以,更新你的公式列表(brew update),然后你需要做的就是:
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.
如果您的系统上没有c编译器,您可能需要安装Xcode或Xcode命令行工具——都是免费的。然而,如果是这样的话,你应该去看看自制啤酒。
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.
#2
83
You can use launchd for that purpose. Launchd can be configured to automatically launch a program when a file path is modified.
您可以为此目的使用launchd。Launchd可以配置为在修改文件路径时自动启动程序。
For example the following launchd config plist will launch the program /usr/bin/logger
when the desktop folder of my user account is modified:
例如,当我的用户帐户的桌面文件夹被修改时,下面的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>
To activate the config plist save it to the LaunchAgents folder in your Library folder as "logger.plist".
要激活配置plist,请将其保存到库文件夹中的LaunchAgents文件夹中,并将其保存为“logger.plist”。
From the shell you can then use the command launchctl
to activate the logger.plist by running:
然后,您可以从shell中使用命令launchctl来激活日志记录器。plist通过运行:
$ launchctl load ~/Library/LaunchAgents/logger.plist
The desktop folder is now being monitored. Every time it is changed you should see an output in the system.log (use Console.app). To deactivate the logger.plist, run:
桌面文件夹现在正在被监视。每次更改时,您应该在系统中看到一个输出。日志(用Console.app)。禁用日志记录器。plist,运行:
$ launchctl unload ~/Library/LaunchAgents/logger.plist
The configuration file above uses the WatchPaths
option. Alternatively you can also use the QueueDirectories
option. See the launchd man page for more information.
上面的配置文件使用watchpath选项。或者,您也可以使用queuedirectory选项。有关更多信息,请参见launchd手册页面。
#3
19
You might want to take a look at (and maybe expand) my little tool kqwait
. Currently it just sits around and waits for a write event on a single file, but the kqueue architecture allows for hierarchical event stacking...
您可能想看看(或者扩展)我的小工具kqwait。目前它只是在一个文件上等待一个写事件,但是kqueue架构允许分层事件堆叠……
#4
19
Facebook's watchman, available via Homebrew, also looks nice. It supports also filtering:
Facebook的watchman也可以通过Homebrew应用,看起来也不错。它还支持过滤:
These two lines establish a watch on a source directory and then set up a trigger named "buildme" that will run a tool named "minify-css" whenever a CSS file is changed. The tool will be passed a list of the changed filenames.
这两行在源目录上建立一个监视,然后设置一个名为“buildme”的触发器,每当CSS文件发生更改时,该触发器将运行一个名为“minify-css”的工具。该工具将被传递一个更改文件名的列表。
$ watchman watch ~/src
美元守望看~ / src
$ watchman -- trigger ~/src buildme '*.css' -- minify-css
$ watchman -触发器~/src build dme '*。css——minify-css
Notice that the path must be absolute.
注意路径必须是绝对的。
#5
13
watchdog is a cross-platform python API for watching files / directories, and it has builtin "tricks" tool that allows you to trigger actions (including shell commands) when events occur (including new added file, removed file and changed file).
watchdog是一个用于监视文件/目录的跨平台python API,具有内置的“技巧”工具,允许在发生事件(包括新添加的文件、删除的文件和更改的文件)时触发操作(包括shell命令)。
#6
4
This is just to mention entr as an alternative on OSX to run arbitrary commands when files change. I find it simple and useful.
这仅仅是在OSX上作为替代,在文件更改时运行任意命令。我发现它既简单又有用。
#7
3
Apple OSX Folder Actions allow you to automate tasks based on actions taken on a folder.
Apple OSX文件夹操作允许您根据文件夹上的操作自动执行任务。
#8
3
Edit: fsw
has been merged into fswatch
. In this answer, any reference to fsw
should now read fswatch
.
编辑:fsw已合并为fswatch。在这个答案中,对fsw的任何引用现在都应该读fswatch。
I wrote an fswatch
replacement in C++ called fsw
which features several improvements:
我在c++中写了一个fswatch的替代品,叫做fsw,它有几个改进:
-
It's a GNU Build System project which builds on any supported platform (OS X v. >= 10.6) with
它是一个GNU构建系统项目,构建在任何支持的平台上(OS X v. >= 10.6)
./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.它的输出易于解析,因此fsw输出可以通过管道传输到另一个进程。
- Latency can be customised with
-l, --latency
. - 延迟可以定制为-l,即延迟。
- Numeric event flags can be written instead of textual ones with
-n, --numeric
. - 数字事件标志可以用-n表示,而不是文本。
- The time format can be customised using
strftime
format strings with-t, --time-format
. - 时间格式可以使用strftime格式字符串使用-t, -时间格式来定制。
- The time can be the local time of the machine (by default) or UTC time with
-u, --utc-time
. - 时间可以是机器的本地时间(默认情况下)或带有-u、- UTC时间的UTC时间。
Getting fsw:
焊:
fsw
is hosted on GitHub and can be obtained cloning its repository:
fsw托管在GitHub上,可以获得其库的克隆:
git clone https://github.com/emcrisostomo/fsw
Installing fsw:
安装的焊:
fsw
can be installed using the following commands:
可以使用以下命令安装fsw:
./configure && make && sudo make install
Further information:
进一步的信息:
I also wrote an introductory blog post where you can find a couple of examples about how fsw
works.
我还写了一篇介绍性的博客文章,在这里你可以找到一些关于fsw如何工作的例子。
#9
2
My fork of fswatch provides the functionality of inotifywait -m
with slightly less (no wait, more! I have a lot more troubles on Linux with inotifywait
...) parse-friendly output.
我的fswatch的fork提供了inotifywait -m的功能,其功能稍微少了一点(没有等待,更多了!)我在Linux上遇到了更多的麻烦,比如inotifywait…)
It is an improvement upon the original fswatch
because it sends out the actual path of the changed file over STDOUT rather than requiring you to provide a program that it forks.
这是对原始fswatch的改进,因为它将修改后的文件的实际路径发送到STDOUT上,而不是要求您提供一个它派生的程序。
It's been rock solid as the foundation of a series of scary bash scripts I use to automate stuff.
它是我用来实现自动化的一系列可怕的bash脚本的基础。
(this is off-topic) inotifywait
on Linux, on the other hand, requires a lot of kludges on top of it and I still haven't figured out a good way to manage it, though I think something based on node.js
might be the ticket.
另一方面,Linux上的inotifywait需要大量的硬件,而且我还没有想出一个好的方法来管理它,尽管我认为这是基于node的。可能是js。
#10
2
Here's a one-liner using sschober
's tool.
这是使用sschober工具的一行代码。
$ while true; do kqwait doc/my_file.md; make; done
#11
1
I have a GIST for this and the usage is pretty simple
我对此有个大致的了解,使用起来也很简单
watchfiles <cmd> <paths...>
To illustrate, the following command will echo Hello World
every time that file1
OR file2
change; and the default interval check is 1 second
举例来说,以下命令将在文件1或文件2每次更改时回显Hello World;默认的间隔检查是1秒
watchfiles 'echo Hello World' /path/to/file1 /path/to/file2
If I want to check every 5 seconds I can use the -t
flag
如果我想每5秒检查一次,我可以使用-t标志
watchfiles -t 'echo Hello World' /path/to/file1 /path/to/file2
-
-v
enables theverbose
mode which shows debug information - -v允许显示调试信息的详细模式
-
-q
makeswatchfiles
execute quietly (#
will be shown so the user can see the program is executing) - -q使watchfiles安静地执行(#将显示,以便用户看到程序正在执行)
-
-qq
makeswatchfiles
execute completely quietly - -qq让watchfiles完全安静地执行
-
-h
shows the help and usage - -h表示帮助和用法
https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55
https://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55
#12
-1
Here's a simple single line alternative for users who don't have the watch
command who want to execute a command every 3 seconds:
以下是一个简单的单行选项,供没有watch命令的用户使用,他们希望每3秒执行一次命令:
while :; do your-command; sleep 3; done
而:;做您的命令;睡眠3;完成
It's an infinite loop that is basically the same as doing the following:
它是一个无限大的循环,基本上和下面做的一样:
watch -n3 your-command
看n3命令