Recursively getting the size of a directory
Asked Answered
D

6

7

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.

Donnydonnybrook answered 19/2, 2012 at 23:59 Comment(2)
Related: #3632574Nebulous
Related: #4509192Nebulous
N
1

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).

Nebulous answered 20/2, 2012 at 0:6 Comment(1)
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
C
5

This seems to work:

Dir.glob(File.join(dir, '**', '*'))
  .map{ |f| File.size(f) }
  .inject(:+)
Conner answered 15/3, 2017 at 6:31 Comment(2)
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
W
2

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'
Weizmann answered 26/6, 2013 at 0:22 Comment(0)
K
2

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
Kaveri answered 15/1, 2016 at 10:7 Comment(0)
N
1

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).

Nebulous answered 20/2, 2012 at 0:6 Comment(1)
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
J
-2

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

http://ruby-doc.org/core-1.9.3/File/Stat.html#method-i-size

Joycelynjoye answered 20/2, 2012 at 0:7 Comment(1)
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
C
-3

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 ...

Sysinternals Suite

Carvel answered 31/12, 2012 at 19:58 Comment(1)
It's not clear how this solves the issue. YOu'll need to explainPrepared

© 2022 - 2024 — McMap. All rights reserved.