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

元气小坏坏 提交于 2019-11-27 13:52:11

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/357754/can-i-traverse-symlinked-directories-in-ruby-with-a-glob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!