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?
Best Answer
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.x