I want to to write a script in either Ruby or Python that will look at the contents of a directory and monitor for
1. Any new files
2. Any saves to existing files.
I'm not bothered about what is different in the files - just that they have been saved or are new.
The script then returns the name of the file (with its full path) perhaps once every 2 seconds or something.
I want to scan files within folders to so
mainfile.txt
images/image1.jpg
images/icons/icon1.png
All these files / folders need to be montitored.
I eventually want to FTP the files that have changed / been added but I'm happy to settle for a solution to the first part of the problem as I know how to ftp (but by all means include this if you want to!)
I am using OSX 10.5.8
watchdog is an open-source python API library that is a cross-platform API to monitor file system events. You can specify a folder or a directory to watchdog observer, which keeps monitoring the folder for any changes like file creation, modification, deletion, or moving of files from one folder to another.
inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.
The inotify interface does have limitations—it can't monitor remote, network-mounted filesystems (that is, NFS); it does not report the userid involved in the event; it does not work with /proc or other pseudo-filesystems; and mmap() operations do not trigger it, among other concerns.
inotify (short for inode notify) is a Linux kernel subsystem that notices changes in a file system (file/directory) and notifies those changes to applications.
For ruby here is a gem which can help you: https://github.com/thibaudgg/rb-fsevent
FSEvent is the system you want to use on OS X and this is a binding bringing it to Ruby, this gem is used by Guard that is how I found it. (https://github.com/guard/guard)
You could even consider using Guard which provide a nice api to monitor files/paths and run action on changes.
Edit: Since I posted this a new gem was released to abstract platform specific stuff: https://github.com/guard/listen , this gem provide a portable api to monitor filesystem events on multiple platforms
Ah, watching FTP. As it happens, I built something like this for work. While mine was dealing with several constraints I hope you don't have, the part that sat on linux was easy. OSX in this day and age is mostly a repackaged linux.
So, you should be able to install iwatch, which uses the inotify kernel feature. If you can't use iwatch itself, which would be very surprising as it's written in perl, then the source should point you to how to directly communicate with inotify. This link has stuff on using python with inotify.
There are a few other alternatives to iwatch, but they all hook into inotify. The only other alternative is to do manual directory scans on a timer, which is clunky and should be avoided. I did it on windows, and it has.... problems. If I ever get the chance, I'll be reworking that section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With