Alex Rivera | Logout

Can I traverse symlinked directories in Ruby with a "**" glob?

Asked 2008-12-10T21:55:44.197
25

In Ruby, Dir.glob("**/*.rb") (for instance) doesn't traverse symlinked directories. Is it possible to get the ** to traverse symlinks?

I'm using two gems which find files this way, but I need them to see files within a symlinked directory.

Edit
Report

1 Answer

42

Jonathan's clever and cunning approach is great, capable of slashing through hordes of symlinks with but a mere flick of a few asterisks, muahaha. However, it has the unfortunate side-effect of not returning immediate-child matches. An improved version might be:

Dir.glob("**{,/*/**}/*.rb")

Which will (in my tests) do both follow one symlink and return immediate children.

answered 2010-04-27T18:48:18.100

Your Answer