Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch directory for file changes

I want to monitor a directory (of thousands of files, with about 5 levels of sub directories) for when files are changed. I know I can use the FSEvents API to monitor a directory for when files change inside that directory, but I can't seem to figure out how to determine which file(s) changed. This reference suggests I build a binary tree and traverse the tree each time an event is triggered, is that the best way to determine which files were changed? If not, what are some better alternatives?

Is it better to recursively scan the directory and attach kqueue to every file? I'm not sure how well that would work on thousands of files?

like image 899
v0idless Avatar asked Jun 26 '12 20:06

v0idless


People also ask

How do I monitor a folder for changes?

Track Events with Event Viewer To monitor changes to a folder, you need to open the Event Viewer. In Windows search box, type “Event Viewer” and open the tool from the result. Go to “Windows Logs” and then “Security”. This will open a list of the recent activities on the middle panel.

Which tool monitors for file changes?

Teramind A file activity monitor that records the users that access or modify any file on the system. PA File Sight A real-time file monitoring system that logs the source of any file changing activity. FileAudit A real-time file monitoring system that includes alerts to key supervisors.

How do I monitor files in a folder?

Details Tab You can use the ellipsis (...) button to browse for the folder. Select this option to monitor the files and folders in sub-folders in the Folder that you specified.


2 Answers

I've used UKKQueue before with mixed results.

I've recently become aware of a better solution, but I haven't tried this. If you only need to target Lion, I think that the new best-practice way to do this is to use an NSFileCoordinator. You implement the methods of NSFilePresenter to indicate which directory you're interested in (the presentedItemURL property) and the system will notify you when a sub item moves/changes/is deleted (via methods like presentedSubitemDidChangeAtURL:)

I'd love to hear how that works out if you do go that route.

like image 102
Jesse Rusak Avatar answered Oct 07 '22 03:10

Jesse Rusak


If you create your stream using kFSEventStreamCreateFlagFileEvents then you will get events for the changes to each file rather than just a notification of the change to the watched directory. Unfortunately this is only available in OSX 10.7 and later.

like image 37
hvanbrug Avatar answered Oct 07 '22 04:10

hvanbrug