22

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.

ashawley
  • 4,195
  • 1
  • 27
  • 40
Peeja
  • 13,683
  • 11
  • 58
  • 77

2 Answers2

38

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.

Tim Harper
  • 2,561
  • 20
  • 23
  • Definitely works as it's referenced in the source code a popular open source project `parralel_spec` https://github.com/grosser/parallel_tests/blob/f680c77335dafe579022da7e76b4a168d92c8bf0/lib/parallel_tests/test/runner.rb#L221-L223 – Dorian Feb 01 '17 at 00:15
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.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199