Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

symlink

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.

like image 630
Peeja Avatar asked Dec 10 '08 21:12

Peeja


2 Answers

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.

like image 139
Tim Harper Avatar answered Nov 03 '22 02:11

Tim Harper


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.

like image 29
Jonathan Lonowski Avatar answered Nov 03 '22 03:11

Jonathan Lonowski