Is there a good gem for getting recursively calculated directory sizes? In unix, I can use du
, but I want a library that absorbs the difference among OS.
Recursively getting the size of a directory
Related: #3632574 –
Nebulous
Related: #4509192 –
Nebulous
Looks like sys-filesystem handles this, but you'll need to do some math to convert the available blocks into bytes (by multiplying by block-size).
This may be due the long time passed since this answer, but the mentioned gem correctly determines the block size (and also size in bytes, cf.
bytes_{used,total,free}
only on a filesystem (=mount) level and does not seem to work to determine the size of a directory. –
Lick This seems to work:
Dir.glob(File.join(dir, '**', '*'))
.map{ |f| File.size(f) }
.inject(:+)
I think you're actually just getting the length of the string with
(&:size)
there. What you want instead is { |file| File.size(file) }
. –
Recreate Or use
sum
instead of inject(:+)
(tested on Ruby 2.6.4) –
Nidorf Could something like this work for you?
def directory_size(path)
path << '/' unless path.end_with?('/')
raise RuntimeError, "#{path} is not a directory" unless File.directory?(path)
total_size = 0
Dir["#{path}**/*"].each do |f|
total_size += File.size(f) if File.file?(f) && File.size?(f)
end
total_size
end
puts directory_size '/etc'
Here's my solution using http://ruby-doc.org/core-2.2.0/File.html#method-c-size:
def directory_size(path)
size=0
Dir.glob(File.join(path, '**', '*')) { |file| size+=File.size(file) }
size
end
Looks like sys-filesystem handles this, but you'll need to do some math to convert the available blocks into bytes (by multiplying by block-size).
This may be due the long time passed since this answer, but the mentioned gem correctly determines the block size (and also size in bytes, cf.
bytes_{used,total,free}
only on a filesystem (=mount) level and does not seem to work to determine the size of a directory. –
Lick Check out the File::Stat
class (note that it does not calculate size of directory contents, it needs to be done manually).
file = File::Stat.new('.')
puts file.size
I assume you were downvoted because this returns the size of the directory, but not the combined size of the contents of the directory. –
Magyar
Support Tools:
diruse /M %windir%
diruse /K /S %windir%
diruse /S %windir%
diruse /, %windir%
Microsoft ... system install CD
msiexec /i %cd:~0,2%\SUPPORT\TOOLS\SUPTOOLS.MSI /q addlocal=all
Sysinternals Suite Utilities:
du.exe -l 1 %windir%
Microsoft ...
It's not clear how this solves the issue. YOu'll need to explain –
Prepared
© 2022 - 2024 — McMap. All rights reserved.