# Ruby equivalent for \`du\`

**URL:** <https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217>\
**Category:** Ruby\
**Created:** [December 5, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217 "2007-12-05T17:51:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_von\_Kleis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@Christian\_von\_Kleis](https://www.ruby-forum.com/u/Christian_von_Kleis)\
**Post date:** [December 5, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217/1 "2007-12-05T17:51:45Z")

</div>

I wrote a simple method for recursively finding the size of a  
directory, because I didn’t see anything in File::Stat or FileUtils  
that does that. Am I missing it? Does Ruby already have an  
equivalent to `du`?

def size(f)  
return File.size(f) unless File.directory?(f)  
Dir.entries(f)[2…-1].inject(0) {|total, s| total +  
size(File.join(f, s))}  
end

---

<div class="post-metadata">

**Author:** ![Christian\_von\_Kleis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@Christian\_von\_Kleis](https://www.ruby-forum.com/u/Christian_von_Kleis)\
**Post date:** [December 5, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217/2 "2007-12-05T18:10:32Z")

</div>

On Dec 5, 10:51 am, Christian von Kleist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I wrote a simple method for recursively finding the size of a  
> directory, because I didn’t see anything in File::Stat or FileUtils  
> that does that. Am I missing it? Does Ruby already have an  
> equivalent to `du`?
> 
> def size(f)  
> return File.size(f) unless File.directory?(f)  
> Dir.entries(f)[2…-1].inject(0) {|total, s| total + size(File.join(f, s))}  
> end

Couple problems with that approach 1.) assuming that “.”, “…” are the  
first elements of the entries array, 2.) for deep directories you’ll  
run out of stack very quickly. Have a look at the example at the top  
of the Find module. 😉

[1] [http://www.ruby-doc.org/core/classes/Find.html](http://www.ruby-doc.org/core/classes/Find.html)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Christian\_von\_Kleis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@Christian\_von\_Kleis](https://www.ruby-forum.com/u/Christian_von_Kleis)\
**Post date:** [December 5, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217/3 "2007-12-05T18:56:16Z")

</div>

On Dec 5, 2007 12:10 PM, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end

Good point! Something like %w(. …).include?(file) would be better  
than relying on . and … to be the first two entries. I’m not worried  
about stack space, though I did consider it. For this application I  
won’t find thousands of levels of subdirectories, and even then I  
probably wouldn’t run out of stack space. I could rewrite the  
recursion as a loop, but…

As you suggested, I should have used Find.find instead. The  
implementation is simpler, more readable, and more robust:

require ‘find’  
def size(f)  
total = 0  
Find.find(f) {|s| total += File.directory?(s) ? 0 : File.size(s)}  
total  
end

(For my application I don’t care about the size of directories since  
those are highly configuration dependent and this script must produce  
the same result with the same file/directory tree on different  
computers.)

Thanks, Jordan!

---

<div class="post-metadata">

**Author:** ![Christian\_von\_Kleis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5a626/32.png) [@Christian\_von\_Kleis](https://www.ruby-forum.com/u/Christian_von_Kleis)\
**Post date:** [December 5, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-for-du/122217/4 "2007-12-05T19:55:39Z")

</div>

On Dec 5, 11:53 am, Christian von Kleist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > equivalent to `du`?
> 
> recursion as a loop, but…
> 
> (For my application I don’t care about the size of directories since  
> those are highly configuration dependent and this script must produce  
> the same result with the same file/directory tree on different  
> computers.)
> 
> Thanks, Jordan!

No problem. 🙂

Regards,  
Jordan
