Created
September 13, 2014 22:36
-
-
Save javierav/d94bd9d8bf6ade145dd5 to your computer and use it in GitHub Desktop.
compare two directories using a sha-256 hash digest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'sequel' | |
require 'digest' | |
CHECK_DIR = '/home/javier/my_dir' | |
DB = Sequel.sqlite('/home/javier/scanner.db') | |
PATHS = Dir.glob(File.join(CHECK_DIR, '**/{*,.*}')) | |
counter = 0 | |
not_found_counter = 0 | |
total = PATHS.length | |
checksums = DB[:checksums] | |
PATHS.each do |file| | |
if File.file? file | |
digest = Digest::SHA256.file( file ).hexdigest | |
unless checksums.first( digest: digest ) | |
puts file | |
not_found_counter += 1 | |
end | |
counter += 1 | |
if counter % 100 == 0 | |
puts "#{counter}/#{total}" | |
end | |
end | |
end | |
puts "#{not_found_counter} files not found!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'sequel' | |
gem 'sqlite3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'sequel' | |
require 'digest' | |
SCAN_DIR = '/home/javier/Dropbox' | |
DB = Sequel.sqlite('/home/javier/scanner.db') | |
unless DB.table_exists?(:checksums) | |
DB.create_table(:checksums) do | |
primary_key :id | |
String :path, text: true | |
String :digest, size: 64 | |
end | |
end | |
PATHS = Dir.glob(File.join(SCAN_DIR, '**/{*,.*}')) | |
counter = 0 | |
total = PATHS.length | |
checksums = DB[:checksums] | |
PATHS.each do |file| | |
if File.file? file | |
digest = Digest::SHA256.file( file ).hexdigest | |
checksums.insert path: file, digest: digest | |
counter += 1 | |
if counter % 100 == 0 | |
puts "#{counter}/#{total}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment