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

2

24

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.

Lieutenant answered 10/12, 2008 at 21:55 Comment(0)
H
41

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.

Hamlett answered 27/4, 2010 at 18:48 Comment(1)
Definitely works as it's referenced in the source code a popular open source project parralel_spec github.com/grosser/parallel_tests/blob/…Hypercatalectic
L
12

Normally not with recursive search due to the risk of infinite loops.

But, this discussion may help:

Dir.glob("**/*/**/b") will follow a symlink up to once.

Lacylad answered 10/12, 2008 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.