Feature #17548
openNeed simple way to include symlink directories in Dir.glob
Description
I would like to suggest that Ruby provide a simple way to glob that includes the content of symlinked directories.
I have my Ruby projects in a ~/work
directory that is symlinked to a directory an another partition.
For example, ~
is on /dev/sda1
, whereas ~/work
is a link to a directory on /dev/sda2
. I need this because I want to operate on the same code bases regardless of which partition I boot from (they are various Linux distros).
The array returned by Dir.glob(Dir.home, '**', '*rb')
does not include any files in ~/work. The size of that array is only 84. In contrast, when I use find ~ -name "*rb" | wc -l
to get the number of files, I get 87,229 files.
I was hoping that one of the flags that can be passed to glob
would help, but the only relevant one I found was to not follow links, which is the opposite of what I wanted.
There are arcane workarounds using fancy glob patterns, but I believe it's important that the language provide a simple way to accomplish this. (For example, the Unix find
command has an -L
option for this.) I understand that it may not be possible to retrofit it into the existing functions (glob
and []
), but even providing a different method (e.g. one named glob_include_links
) would be ok. Or perhaps a glob2
method could be added that would include simple ways to specify that files in hidden directories should be included, in addition to the option to follow symlinks.
I admit that I have no idea how much effort this would be to implement, especially regarding Windows compatibility, but this would be nice to have.
Updated by keithrbennett (Keith Bennett) almost 4 years ago
A clarifying comment that corrects something in my original post but does not change my recommendation:
I wish there a way for me to correct my original post, but I don't see a way to do that. I wanted to delete this text:
"The size of that array is only 84. In contrast, when I use find ~ -name "*rb" | wc -l to get the number of files, I get 87,229 files."
I mistakenly believed that find
's default behavior was to include symlinked directories, but that difference was instead due to the fact that it includes hidden directories by default. Specifically, the ~/.rvm
directory.
The correct way to compare was to use find
's -L
option, which would have includes the symlinked directory and reports a greater number of files than the Dir.glob
number.
Updated by keithrbennett (Keith Bennett) almost 4 years ago
By the way, this is what I was referring to when I mentioned "arcane workarounds":
https://stackoverflow.com/questions/357754/can-i-traverse-symlinked-directories-in-ruby-with-a-glob
(Dir.glob("**{,/*/**}/*.rb")
)
Updated by Dan0042 (Daniel DeLorme) almost 4 years ago
For your specific case, a workaround might be to use a bind mount instead of symlink.
But in general I agree having a follow_symlink
option would be nice to have, either in Dir.glob
or in Find.find
Updated by mame (Yusuke Endoh) almost 4 years ago
zsh uses ***
as a wildcard that expands symlinks. And matz accepted ***
. Maybe nobu will implement it.
Updated by Dan0042 (Daniel DeLorme) almost 3 years ago
Bump.
Today I just came across a case where I wanted to use this feature.
@nobu (Nobuyoshi Nakada) according to dev meeting notes you said "will commit", which implies to me this was already coded? Not sure.