Whoo! I haven't done a post that long in a while!

I put 'activerecord' in there to take advantage of the '24.hours.ago' notation, which makes life much easier. The cost is a little less than 2 seconds to load the library, so I think it's worth it. It runs as a daily job before backup to tape, to clear old backups from the directory tree.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
require 'activerecord'

def delete_recursively(in_here)
  Dir.chdir(in_here)
  Dir.glob('*') do |filename|
    if File.directory?(filename)
      delete_recursively(filename)
      else
        if File.mtime(filename) > 24.hours.ago
        File.delete(filename)
      end
    end
  end
Dir.chdir('..')
end

delete_recursively("your/path/here")