Getting more familiar with Ruby

23Nov08

My wisdom tooth has stopped giving me grief, and I am pretty happy with myself after successfully writing a tiny script to do something I’ve been meaning to do in a while. I attempted this in Python previously and couldn’t get it to work, which makes Ruby quite impressive to me given I only picked it up recently. For those not familiar with .pacsave files, they are configuration files saved by the Arch Linux package manager when the package associated with them is uninstalled.

#!/usr/bin/env ruby
#
# Finds all .pacsave files and deletes them!

require "fileutils"

$etc_location = "/etc/"


def recurse_check(folder)
  listfiles = File.join(folder, "*") 

  #glob gets all files which match the criteria passed in the argument--in this case, all files in folder
  Dir.glob(listfiles).each do |x|
    if File.file?(x) and File.basename(x).to_s.end_with?(".pacsave") #is it a file and does it end with .pacsave?
      puts "File: #{x}"
      FileUtils.rm(x) #removes .pacsave files! Be careful with this!
    elsif File.directory?(x)
      #puts "Directory: #{x}"
      recurse_check(x)
    end
  end

end


recurse_check($etc_location)
Advertisements


%d bloggers like this: