Skip to content

Command line utility to perform actions when files are updated, added or deleted. No config files necessary. Pure ruby implementation and minimalistic Ruby API.

License

Notifications You must be signed in to change notification settings

gcman105/filewatcher

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Filewatcher

Simple file watcher. Monitors changes in filesystem by polling. No dependencies.

Install

Needs Ruby and Rubygems:

$ [sudo] gem install filewatcher

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

Run the echo command when the file myfile is changed:

$ filewatcher "myfile" "echo 'myfile has changed'"

Run any javascript in the current folder when it is updated in Windows Powershell:

> filewatcher *.js "node %FILENAME%"

In Linux/OSX:

$ filewatcher *.js "node $FILENAME"

Run the ruby script when it is changed:

$ filewatcher -e ruby-script.rb

Works with ruby, perl, python and awk by detecting file extensions.

Print a list of all files matching *.css first and the print filename when a file is beeing updated:

$ filewatcher -l *.css 'echo file: $FILENAME'

Available enviroment variables

The environment variable $FILENAME is available in the command. On unix like systems the command has to be enclosed in single quotes. To run node whenever a javascript file is update:

$ filewatcher *.js 'node $FILENAME'

The environment variables $FILEPATH and $FILEDIR is also available.

Command line options

--interval, -i <f>:   Interval in seconds to scan filesystem. Defaults to 0.5 seconds.
        --exec, -e:   Execute file as a script when file is updated.
 --recurse, -r <s>:   Recurse into the directory, watching everything matching 'expression'
 --include, -n <s>:   Include files (default: *)
 --exclude, -x <s>:   Exclude file(s) matching (default: "")
        --list, -l:   Print name of files being watched
     --version, -v:   Print version and exit
        --help, -h:   Show this message

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 the default once every second:

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

To detect if a file is updated or deleted:

FileWatcher.new(["README.rdoc"]).watch() do |filename, event|
  if(event == :changed)
    puts "File updated: " + filename
  end
  if(event == :delete)
    puts "File deleted: " + filename
  end
  if(event == :new)
    puts "New file: " + filename
  end
end

TODO

The Ruby API is fairly well tested but the command line program is buggy at the time beeing.

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

Globbing by Kristoffer Roupé github.com/kitofr

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 - 2014 Thomas Flemming. See LICENSE for details.

About

Command line utility to perform actions when files are updated, added or deleted. No config files necessary. Pure ruby implementation and minimalistic Ruby API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published