here is an ancient ruby script i use for that, it also considers gzip'd files uncompressed but you could comment that out:
cfp:~ > dirsum src/ruby
src/ruby:
b: 768067279.000
kb: 750065.702
mb: 732.486
cfp:~ > cat bin/dirsum
#! /usr/bin/env ruby
at_least_one = false
ARGV.each do |dir|
begin
sum = 0
Dir["#{ dir }/**/**"].each do |path|
if test ?f, path
p path if $DEBUG
begin
case path
when /\.(?:gz|z)\s*$/io
cmd = "{ gzip -l #{ path } | tail -1 | awk '{print $2}' ;} 2>/dev/null"
out = `#{ cmd }`
raise unless $?.exitstatus == 0
sum += out.to_i
else
sum += File.stat(path).size
end
rescue
warn "failed to sum <#{ path }>"
end
end
end
units = [
[:b , (2 ** 0)],
[:kb , (2 ** 10)],
[:mb , (2 ** 20)],
[:gb , (2 ** 30)],
[:tb , (2 ** 40)],
[:pb , (2 ** 50)],
]
puts "#{ dir }:"
units.each do |(label, size)|
q = sum / size.to_f
printf " %s: %.3f\n", label, q if q >= 1
end
at_least_one = true
rescue => e
warn "failed to sum <#{ dir }>"
next
end
end
abort "#{ $0 } dir [dir]+" unless at_least_one
a @ http://codeforpeople.com/
···
On Aug 29, 2008, at 4:56 AM, Thibaut Barrère wrote:
Hi guys,
I'm computing an approximate folder size by summing the sizes of its
files, recursively, with the following code:
def item_size(name)
if File.directory?(name)
(Dir.entries(name) - ['.','..']).inject(0) { |sum,file| sum +
item_size(File.join(name,file)) }
else
File.size(name)
end
end
Another version would be:
def item_size_2(name)
Dir[name+'/**/*'].inject(0) { |sum,file| sum + (File.directory?
(file) ? 0 : File.size(file)) }
end
can anyone think of a shorter or more readable way of writing it ? Do
you know a library that would do this ?
cheers,
Thibaut Barrère / LoGeek
--
http://blog.logeek.fr - learning content for developers
http://evolvingworker.com - tools for a better day
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama