# Standard IO directory surf

**URL:** <https://www.ruby-forum.com/t/standard-io-directory-surf/111686>\
**Category:** Ruby\
**Created:** [August 9, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686 "2007-08-09T20:47:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 9, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/1 "2007-08-09T20:47:45Z")

</div>

how would i go about surfing a whole directory instead of using a  
Dir.glob(\*/\*\*)  
Basically how would i do it using the Find method. I need to surf the  
entire file system and pull up my total number of files that arnt an  
actual directory.

Thanks,

-Jon

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 9, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/2 "2007-08-09T21:28:13Z")

</div>

oh and i also want to pipe the find… find -blah -blah | ./program.rb  
— how would i go about doing this?

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 9, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/3 "2007-08-09T21:42:53Z")

</div>

Jon H. wrote:

> how would i go about surfing a whole directory instead of using a  
> Dir.glob(\*/\*\*)  
> Basically how would i do it using the Find method. I need to surf the  
> entire file system and pull up my total number of files that arnt an  
> actual directory.  
> Three ways (at least) to do this:

1: Using Dir.glob:  
file\_list = Dir.glob(’\*\*/\*’).select{|filename|  
File.file?(filename)}

2: Using Find:  
file\_list = []  
Find.find(’/’) {|path| file\_list \<\< path if File.file? path}

3: Using /usr/bin/find:  
file\_list = `find . -type f`.split

Once you’ve got file\_list, you can take file\_list.length to get the  
number of files. If you want to avoid having an intermediate array, you  
could use Find this way:

file\_count = 0  
Find.find(’/’) {|path| file\_count += 1 if File.file? path}

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 9, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/4 "2007-08-09T21:54:54Z")

</div>

On Aug 9, 2007, at 12:47 PM, Jon H. wrote:

> how would i go about surfing a whole directory instead of using a  
> Dir.glob(\*/\*\*)  
> Basically how would i do it using the Find method. I need to surf the  
> entire file system and pull up my total number of files that arnt an  
> actual directory.

[http://drawohara.tumblr.com/post/8166787](http://drawohara.tumblr.com/post/8166787)

cheers.

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 10, 2007, 6:40am UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/5 "2007-08-10T06:40:34Z")

</div>

On Fri, 10 Aug 2007 04:28:21 +0900, Jon H. wrote:

> oh and i also want to pipe the find… find -blah -blah | ./program.rb  
> — how would i go about doing this?

do it without ruby and just use the shell?

find -type f | ./program.rb

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 10, 2007, 10:20am UTC](https://www.ruby-forum.com/t/standard-io-directory-surf/111686/6 "2007-08-10T10:20:53Z")

</div>

2007/8/9, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> how would i go about surfing a whole directory instead of using a  
> Dir.glob(\*/\*\*)  
> Basically how would i do it using the Find method. I need to surf the  
> entire file system and pull up my total number of files that arnt an  
> actual directory.

non\_directory\_count = Integer `find / -not -type d | wc -l`

Of course, the command in backticks also works on a shell prompt - if  
you just need that count. (btw, I used Integer to detect situations  
where the command does not work and return an improper result (i.e. an  
empty string).

Kind regards

robert
