Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Ruby's Find.find follow symlinks?

Tags:

find

ruby

symlink

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?

like image 463
arnab Avatar asked Oct 20 '10 01:10

arnab


People also ask

How do I get a list of symbolic links?

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.

Can rsync create symlinks?

"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.

What is the symbolic link in Nodejs?

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.


1 Answers

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.

like image 189
user3055549 Avatar answered Oct 14 '22 01:10

user3055549