Created
September 23, 2011 11:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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