# How to parse directory structure using ruby

**URL:** <https://www.ruby-forum.com/t/how-to-parse-directory-structure-using-ruby/126543>\
**Category:** Ruby\
**Created:** [January 19, 2008, 9:06am UTC](https://www.ruby-forum.com/t/how-to-parse-directory-structure-using-ruby/126543 "2008-01-19T09:06:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurabhpurnaye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@saurabhpurnaye](https://www.ruby-forum.com/u/saurabhpurnaye)\
**Post date:** [January 19, 2008, 9:06am UTC](https://www.ruby-forum.com/t/how-to-parse-directory-structure-using-ruby/126543/1 "2008-01-19T09:06:21Z")

</div>

Hi all,  
Greetings…  
In my project i have stuck a place … where i have to parse directory  
structure.  
Can you please help me out in this ?  
The requirement is like… i will give input as name of directory…  
The program should act like to parse the whole directory structure and  
give me output as, with all the directories/files with the name…

---

<div class="post-metadata">

**Author:** ![saurabhpurnaye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@saurabhpurnaye](https://www.ruby-forum.com/u/saurabhpurnaye)\
**Post date:** [January 19, 2008, 9:49am UTC](https://www.ruby-forum.com/t/how-to-parse-directory-structure-using-ruby/126543/2 "2008-01-19T09:49:48Z")

</div>

Saurabh P. wrote:

> Hi all,  
> Greetings…  
> In my project i have stuck a place … where i have to parse directory  
> structure.  
> Can you please help me out in this ?  
> The requirement is like… i will give input as name of directory…  
> The program should act like to parse the whole directory structure and  
> give me output as, with all the directories/files with the name…

Check out the “find” extension in the standard library.

## ri Find#find -------------------------------------------------------------- Find#find find(\*paths) {|path| …}

```
  Calls the associated block with the name of every file and
  directory listed as arguments, then recursively on their
  subdirectories, and so on.

  See the Find module documentation for an example.
```

---

<div class="post-metadata">

**Author:** ![saurabhpurnaye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@saurabhpurnaye](https://www.ruby-forum.com/u/saurabhpurnaye)\
**Post date:** [January 19, 2008, 11:51am UTC](https://www.ruby-forum.com/t/how-to-parse-directory-structure-using-ruby/126543/3 "2008-01-19T11:51:11Z")

</div>

On 19.01.2008 09:49, Tim H. wrote:

> Check out the “find” extension in the standard library.
> 
> ## ri Find#find -------------------------------------------------------------- Find#find find(\*paths) {|path| …}
> 
> ```
> Calls the associated block with the name of every file and
> directory listed as arguments, then recursively on their
> subdirectories, and so on.
> 
> See the Find module documentation for an example.
> 
> ```

And then there is of course Dir[] as well. For simple outputting it’s  
probably easier:

ruby -e ‘puts Dir["\*\*/\*"]’

Btw, the process is not called “parsing”. In this case I’d rather use  
the term “traversing”. “Parsing” usually means read a byte sequence and  
determine its structure according to some grammar.

Kind regards

robert
