Skip to content

Instantly share code, notes, and snippets.

@alexvbush
Created September 23, 2011 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexvbush/1237166 to your computer and use it in GitHub Desktop.
Save alexvbush/1237166 to your computer and use it in GitHub Desktop.
Recursively calculates the size of a folder(path to folder is the first input in terminal). Found other solutions using system calls but decide to write my own.
if ARGV.count != 1 || !File.directory?(ARGV[0])
p 'Please specify a folder.'
exit
end
def calculate_size(file)
#p 'calculating ' + file
if File.directory? file
i = 0
Dir.open(file).each do |f|
i += calculate_size(file + "/" + f).to_i unless f == '.' || f == '..'
end
#p "#{file} directory size: " + i.to_s
return i
else
#p "#{file} file size: " + File.size?(file).to_s
return File.size?(file)
end
end
size = calculate_size(ARGV[0])
p "#{size.to_f / 1024 / 1024} Mb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment