Agefiles: Difference between revisions
m (New page: My script to delete old files <geshi lang="ruby"> #!/bin/env ruby require 'find' require 'optparse' dryrun = false args = OptionParser.new do |opts| opts.banner = "Usage: #{$0} [opti...) |
mNo edit summary |
||
Line 48: | Line 48: | ||
puts "deleted #{fcount} files totalling #{bcount} bytes" | puts "deleted #{fcount} files totalling #{bcount} bytes" | ||
</geshi> | </geshi> | ||
[[Category:Ruby]] |
Revision as of 00:37, 31 July 2008
My script to delete old files
<geshi lang="ruby">
- !/bin/env ruby
require 'find' require 'optparse'
dryrun = false
args = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] [directory] [size]"
opts.on("-n", "--dry-run", "show what would have been deleted") do |v| dryrun = true end
end.parse!
DIR = args[0] || '.' File.directory?(DIR) || raise("#{DIR} is not a directory") MAXSIZE = (args[1] || 1000000).to_i
files = [] total = 0 Find.find(DIR) do |f|
if File.file?(f) then files << [ File.mtime(f), File.size(f), f ] total += File.size(f) end
end
files = files.sort_by { |x| x[0] }
puts "#{DIR} is currently size #{total} and needs to be no more than #{MAXSIZE}" fcount=0 bcount=0 while total > MAXSIZE && (item = files.shift) do
total -= item[1] bcount += item[1] if dryrun then puts "unlink #{item[2]}" else File.unlink(item[2]) end fcount += 1
end
puts "deleted #{fcount} files totalling #{bcount} bytes" </geshi>