> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/w/walk-a-directory/recursively.md).

# Recursively

```ruby
func traverse(Block callback, Dir dir) {
    dir.open(\var dir_h) || return nil
 
    dir_h.entries.each { |entry|
        if (entry.is_a(Dir)) {
            traverse(callback, entry)
        } else {
            callback(entry)
        }
    }
}
 
var dir = Dir.cwd
var pattern = /foo/   # display files that contain 'foo'
 
traverse(
    { |file|
        if (file.basename ~~ pattern) {
            say file
        }
    } => dir
)
```
