Cool Ruby script: Check for changed files

No Starch Press –

Are you spending valuable time on work a well-trained monkey could do? If so, Wicked Cool Ruby Scripts will teach you how to automate repetitive tasks using Ruby, one of the most powerful and easy-to-use programming languages around.

This excerpt is from Wicked Cool Ruby Scripts by Steve Pugh, published by No Starch Press.


In any programming language, scripting is the solution to frequently performed tasks. If you find yourself asking Couldn't a robot or well-trained monkey do this job?, then scripting with Ruby just might be the next best solution. Writing scripts for frequently performed tasks makes your job and computing experience as efficient as it can be. Who wouldn't want to get the job done in less time with less effort? As you work through these examples, I encourage you to write down ideas for your own scripts. Once you've finished this book, you will probably have a list of scripts you want to write, or at the very least, some useful revisions of mine. Are you ready? Let's get started!

#1 Check for Changed Files


The purpose of this script is to validate a file's integrity. While it sounds like a humble end use, its applications are broad: If you can't trust the contents of files on your computer, you can't trust your computer. Would you know if a malicious worm or virus modified a file on your system? If you think your antivirus has you covered, think again -- most only go as far as checking for known viruses and their signatures. File integrity validation is used every day for real-world tasks such as digital forensics and tracking the behavior of malicious logic. One method of tracking file integrity is shown below.

The Code

require 'find'
require 'digest/md5'
unless ARGV[0] and[0])
puts "\n\n\nYou need to specify a root directory: changedFiles.rb


root = ARGV[0]
oldfile_hash =
newfile_hash =
file_report = "#{root}/analysis_report.txt"
file_output = "#{root}/file_list.txt"
oldfile_output = "#{root}/file_list.old"


if File.exists?(file_output)
File.rename(file_output, oldfile_output), 'rb') do |infile|
while (temp = infile.gets)
line = /(.+)\s{5,5}(\w{32,32})/.match(temp)
puts "#{line[1]} ---> #{line[2]}"
oldfile_hash[line[1]] = line[2]


Find.find(root) do |file|
next if /^\./.match(file)
next unless File.file?(file)
newfile_hash[file] = Digest::MD5.hexdigest(
puts "Error reading #{file} --- MD5 hash not computed."
report =, 'wb')
changed_files =, 'wb')
newfile_hash.each do |file, md5|
changed_files.puts "#{file}     #{md5}"

(4) { |file| newfile_hash[file] == oldfile_hash[file]
}.each do |file|


newfile_hash.each do |file, md5|
report.puts "#{oldfile_hash[file] ? "Changed" : "Added"} file: #{file}


oldfile_hash.each do |file, md5|
report.puts "Deleted/Moved file: #{file} #{md5}"

Running the Code

Execute this script by typing:

ruby changedFiles.rb /path/to/check/

You can add more than one directory to crawl, but subdirectories will automatically be verified. The script will automatically determine if a directory exists and then add it to the crawler's queue.

1 2 Page 1
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon