Skip to content

Instantly share code, notes, and snippets.

@willnet
Created May 30, 2010 11:42
Show Gist options
  • Save willnet/418966 to your computer and use it in GitHub Desktop.
Save willnet/418966 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# ログローテーションでできた古いログファイルを整理するためのスクリプト
# script/runner で実行する
Dir.chdir(RAILS_ROOT + "/log")
today = DateTime.now.strftime("%Y%m%d")
thismonth = DateTime.now.strftime("%Y%m")
Dir.glob("*").each do |filename|
if filename =~ /\d{8}/ && filename !~ /#{thismonth}\d\d/
dirname = filename[/(\d{6})\d\d/, 1]
unless FileTest.exist?(dirname)
Dir.mkdir(dirname)
puts "mkdir #{dirname}"
end
to = "#{dirname}/#{filename}"
puts "mv #{filename} #{to}"
File.rename(filename, to)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment