I have a file hierarchy and some of the sub-directories are relative symlinks. I am using Ruby's Find.find
to crawl through these dirs and find some specific files. However it's not looking into any directory which is a symlink (it follows files which are symlinks).
Looking at the source code it seems the problem is because it's using File.lstat(file).directory?
to test if something is a directory. This returns false
for symlinks but File.stat.directory?
returns true
.
How can I make Find.find
follow symlinks, short of monkey patching it to use File.stat
instead of File.lstat
?
Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.
"Copy symlinks as symlinks" means exactly what it says: If rsync sees a symlink in the source directory, it will create an identical symlink in the destination.
symlink() method is used to create a symlink to the specified path. This creates a link making the path point to the target . The relative targets are relative to the link's parent directory.
I came across the similar situation and decided to follow the real path without extra gem.
require 'find'
paths = ARGV
search_dirs = paths.dup
found_files = Array.new
until search_dirs.size == 0
Find.find( search_dirs.shift ) do |path|
if File.directory?( path ) && File.symlink?( path )
search_dirs << File.realdirpath( path )
else
found_files << path
end
end
end
puts found_files.join("\n")
This way can't keep the original path with symbolic link but is fine for me at the moment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With