Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created October 6, 2011 01:43
Show Gist options
  • Save yuya-takeyama/1266280 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/1266280 to your computer and use it in GitHub Desktop.
ローカルとリモートのファイル間で MD5 を比較し, 一致しないものを出力する.
#!/usr/bin/env ruby
# coding: utf-8
$KCODE = 'u'
=begin
ローカルとリモートのファイル間で MD5 を比較し,
一致しないものを出力する.
Usage: ruby find_diffs.rb target_host target_directory [comma_separated_files_to_ignore]
Author: Yuya Takeyama
=end
# ローカルファイルの MD5 を取得する.
def local_hash(f)
`md5sum #{f}`[0, 32]
end
# リモートファイルの MD5 を取得する.
def remote_hash(f, host)
`ssh #{host} md5sum #{f} 2> /dev/null`[0, 32]
end
# ディレクトリ内のファイルを再帰的に走査する.
def dir_walk(dir, ignore, &block)
Dir::entries(dir).each do |f|
unless ignore.include? f
f = dir + "/" + f
if FileTest::directory?(f)
dir_walk(f, ignore, &block)
else
yield f
end
end
end
end
if ARGV.size < 2
puts "Usage: ruby find_diffs.rb target_host target_directory [comma_separated_files_to_ignore]"
end
host = ARGV[0]
dir = ARGV[1].gsub(%r{/+$}, '')
ignore = ['.', '..']
ignore.concat(ARGV[2].split(',')) if ARGV[2]
dir_walk(dir, ignore) do |f|
puts f if local_hash(f) != remote_hash(f, host)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment