Skip to content

Instantly share code, notes, and snippets.

@daicham daicham/gist:2145516
Created Mar 21, 2012

Embed
What would you like to do?
Print the directory structure and its actual size
def print_tree(base_dir)
base_dir = base_dir + '/' unless base_dir.end_with? '/'
size = 0
Dir.glob("#{base_dir}*/") do |dir|
#puts "entering #{dir}"
size += print_tree dir
end
Dir.glob("#{base_dir}*") do |file|
real_size = File.size file
size += compute_actual_size real_size
#puts "#{file} : #{File.size file}"
end
puts "#{base_dir}:#{h size}"
size
end
BLOCK_SIZE = 1024
def compute_actual_size real_size
real_size % BLOCK_SIZE == 0 ?
real_size :
real_size - (real_size % BLOCK_SIZE) + BLOCK_SIZE
end
def h(bytes, count=0)
#puts bytes, count
if bytes >= 1024
h(bytes / 1024.0, count+1)
else
case count
when 0
"%#.2f bytes" % bytes
when 1
"%#.2f KB" % bytes
when 2
"%#.2f MB" % bytes
when 3
"%#.2f GB" % bytes
when 4
"%#.2f TB" % bytes
else
"unknown"
end
end
end
$*.each do |arg|
print_tree arg
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.