In OS X, is there a way (shell script-ish?) to watch a directory for a file pattern and if there are any file changes to execute a command?

  • Although not needed, it might help to have an example: Which files do you want to watch and what should be done with them? Are you limited to using the command line or can you use AppleScript as well?
    – slhck
    Jul 8 '11 at 10:54

There's an app for that:

/System/Library/CoreServices/Folder Actions Setup.app

Select a folder to watch and a script to be executed. You can also edit the script.



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

launchctl load ~/Library/LaunchAgents/watch.and.lol.plist or relogin to load the agent.

There's a launchd gotcha « Managing OS X though:

It turns out that if a file is listed as a launchd job WatchPath, it must exist at all times, or launchd removes the path from its list of paths to watch. So you can't watch for the creation of a certain file; you can only watch for changes to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.