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.

wcruby_thm.jpg

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

changedFiles.rb

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 File.directory?(ARGV[0])
puts "\n\n\nYou need to specify a root directory: changedFiles.rb
\n\n\n"
exit
end

(1)

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

(2)

if File.exists?(file_output)
File.rename(file_output, oldfile_output)
File.open(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]
end
end
end

(3)

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

(4)

newfile_hash.keys.select { |file| newfile_hash[file] == oldfile_hash[file]
}.each do |file|
newfile_hash.delete(file)
oldfile_hash.delete(file)
end

(5)

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

(6)

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

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
What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies