Cool Ruby script: Check for changed files

No Starch Press |  Development, book, Ruby Add a new comment

AttachmentSize
wcruby_ch1.pdf199.41 KB

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

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.

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question