Search code examples
rubyglobdirectory

find . -type f in ruby


I want to grab a list of all the files under a particular directory. Dir.glob works great, but there doesn't seem to be a way to limit results to just files (excluding directories).

Heres's what I have right now:

files = Dir.glob('my_dir/**/*').reject { |f| File.directory?(f) }

Is there a more elegant way to accomplish this?


Solution

  • That's actually a fairly efficient way to go about doing it, but you might also use the Find module:

    require 'find'
    
    found = [ ]
    
    Find.find(base_path) do |path|
      found << path if (File.file?(path))
    end