免责声明:这可能是矫枉过正,但另一方面可能更快实施和更稳定。
有一个名为directory_watcher的 ruby 库,它可以监视目录的更改。
一个简单的脚本,例如
#!/usr/bin/env ruby
require 'rubygems'
require 'directory_watcher'
dw = DirectoryWatcher.new '.'
dw.add_observer do
|*args| args.each do |event|
puts event
end
end
dw.start
gets # when the user hits "enter" the script will terminate
dw.stop
可以让你开始。在这里,修改(或删除或添加)的文件名只是打印到标准输出。
这是一个示例脚本,它将监视file1.txt
, file2.txt
, file3.txt
. 每当其中一个发生更改时,它会将它们连接到files-combined.txt
.
#!/usr/bin/env ruby
require 'rubygems'
require 'directory_watcher'
dw = DirectoryWatcher.new '.'
dw.interval = 1.0
dw.add_observer do |*args|
args.each do |event|
if /file\d/ =~ event.path
`cat file1.txt file2.txt file3.txt > files-combined.txt`
puts "#{Time.now.strftime("%I:%M:%S")} \
Created files-combined.txt (since #{event.path} #{event.type})"
end
end
end
dw.start
gets # when the user hits "enter" the script will terminate
dw.stop
输出如下:
$ ruby 7083085.rb
08:55:47 Created files-combined.txt (since ./file3.txt added)
08:55:47 Created files-combined.txt (since ./file1.txt added)
08:55:47 Created files-combined.txt (since ./file2.txt added)
08:55:54 Created files-combined.txt (since ./file1.txt modified)
08:55:57 Created files-combined.txt (since ./file1.txt modified)