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:
The older way for versions 0.x:
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:Installation without Homebrew
Type these commands in
Terminal.app
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.xApple OSX Folder Actions allow you to automate tasks based on actions taken on a folder.
Edit:
fsw
has been merged intofswatch
. In this answer, any reference tofsw
should now readfswatch
.I wrote an
fswatch
replacement in C++ calledfsw
which features several improvements:It's a GNU Build System project which builds on any supported platform (OS X v. >= 10.6) with
Multiple paths can be passed as different arguments:
It dumps a detailed record with all the event information such as:
Its output is easy to parse so that
fsw
output can be piped to another process.-l, --latency
.-n, --numeric
.strftime
format strings with-t, --time-format
.-u, --utc-time
.Getting fsw:
fsw
is hosted on GitHub and can be obtained cloning its repository:Installing fsw:
fsw
can be installed using the following commands:Further information:
I also wrote an introductory blog post where you can find a couple of examples about how
fsw
works.You can use launchd for that purpose. Launchd can be configured to automatically launch a program when a file path is modified.
For example the following launchd config plist will launch the program
/usr/bin/logger
when the desktop folder of my user account is modified:To activate the config plist save it to the LaunchAgents folder in your Library folder as "logger.plist".
From the shell you can then use the command
launchctl
to activate the logger.plist by running: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:
The configuration file above uses the
WatchPaths
option. Alternatively you can also use theQueueDirectories
option. See the launchd man page for more information.My fork of fswatch provides the functionality of
inotifywait -m
with slightly less (no wait, more! I have a lot more troubles on Linux withinotifywait
...) parse-friendly output.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.It's been rock solid as the foundation of a series of scary bash scripts I use to automate stuff.
(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 onnode.js
might be the ticket.I have a GIST for this and the usage is pretty simple
To illustrate, the following command will echo
Hello World
every time thatfile1
ORfile2
change; and the default interval check is 1 secondIf I want to check every 5 seconds I can use the
-t
flag-v
enables theverbose
mode which shows debug information-q
makeswatchfiles
execute quietly (#
will be shown so the user can see the program is executing)-qq
makeswatchfiles
execute completely quietly-h
shows the help and usagehttps://gist.github.com/thiagoh/5d8f53bfb64985b94e5bc8b3844dba55