Filewatcher

Simple file watcher. Monitors changes in files by polling files.

Command line utility

Filewatcher scans filesystem and execute shell commands when files changes.

Usage:

filewatcher [-i interval] "<filename>" "<shell command>"

Where

filename: filename(s) to scan.
shell command: shell command to execute when file changes on disk.

Examples:

filewatcher "myfile" "echo 'myfile has changed'"
filewathcer -i 2 "*.rb ../src/*.rb" "ruby run_tests.rb"

Options:

--interval, -i <f>:   Interval to scan filesystem. Defaults to 0.5 seconds.
     --version, -v:   Print version and exit
        --help, -h:   Show this message

Install

sudo gem install filewatcher

You need ruby and rubygems installed.

Ruby API

Examples:

Watch a list of files and directories:

require 'filewatcher'

FileWatcher.new(["lib/", "Rakefile"]).watch do |filename|
  puts "Updated " + filename
end

Print the names of files beeing watched before we begin:

FileWatcher.new(["lib/"],"Watching files:").watch do |filename|
  puts "Updated " + filename
end
=> Watching files:
lib/filewatcher.rb

To check for changes more often than once every second:

FileWatcher.new(["README.rdoc"]).watch(0.5) do |filename|
  puts "Updated " + filename
end

Credits

Code inspired by Tom Lieber’s blogg posting: alltom.com/pages/detecting-file-changes-with-ruby

Find method by c00lrguy: snippets.dzone.com/posts/show/5457

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2011 Thomas Flemming. See LICENSE for details.