One-liner to recursively list directories in Ruby?

RubyFilesystemsRecursion

Ruby Problem Overview


What is the fastest, most optimized, one-liner way to get an array of the directories (excluding files) in Ruby?

How about including files?

Ruby Solutions


Solution 1 - Ruby

Dir.glob("**/*/") # for directories
Dir.glob("**/*") # for all files

Instead of Dir.glob(foo) you can also write Dir[foo] (however Dir.glob can also take a block, in which case it will yield each path instead of creating an array).

Ruby Glob Docs

Solution 2 - Ruby

I believe none of the solutions here deal with hidden directories (e.g. '.test'):

require 'find'
Find.find('.') { |e| puts e if File.directory?(e) }

Solution 3 - Ruby

For list of directories try

Dir['**/']

List of files is harder, because in Unix directory is also a file, so you need to test for type or remove entries from returned list which is parent of other entries.

Dir['**/*'].reject {|fn| File.directory?(fn) }

And for list of all files and directories simply

Dir['**/*']

Solution 4 - Ruby

Fast one liner

Only directories

`find -type d`.split("\n")

Directories and normal files

`find -type d -or -type f`.split("\n")`

Pure beautiful ruby

require "pathname"

def rec_path(path, file= false)
  puts path
  path.children.collect do |child|
    if file and child.file?
      child
    elsif child.directory?
      rec_path(child, file) + [child]
    end
  end.select { |x| x }.flatten(1)
end

# only directories
rec_path(Pathname.new(dir), false)
# directories and normal files
rec_path(Pathname.new(dir), true)

Solution 5 - Ruby

As noted in other answers here, you can use Dir.glob. Keep in mind that folders can have lots of strange characters in them, and glob arguments are patterns, so some characters have special meanings. As such, it's unsafe to do something like the following:

Dir.glob("#{folder}/**/*")

Instead do:

Dir.chdir(folder) { Dir.glob("**/*").map {|path| File.expand_path(path) } }

Solution 6 - Ruby

In PHP or other languages to get the content of a directory and all its subdirectories, you have to write some lines of code, but in Ruby it takes 2 lines:

require 'find'
Find.find('./') do |f| p f end

this will print the content of the current directory and all its subdirectories.

Or shorter, You can use the ’**’ notation :

p Dir['**/*.*']

How many lines will you write in PHP or in Java to get the same result?

Solution 7 - Ruby

Here's an example that combines dynamic discovery of a Rails project directory with Dir.glob:

dir = Dir.glob(Rails.root.join('app', 'assets', 'stylesheets', '*'))

Solution 8 - Ruby

Although not a one line solution, I think this is the best way to do it using ruby calls.

First delete all the files recursively
Second delete all the empty directories

Dir.glob("./logs/**/*").each { |file| File.delete(file) if File.file? file }
Dir.glob("./logs/**/*/").each { |directory| Dir.delete(directory) }

Solution 9 - Ruby

Dir.open(Dir.pwd).map { |h| (File.file?(h) ? "#{h} - file" : "#{h} - folder") if h[0] != '.' }

dots return nil, use compact

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLanceView Question on Stackoverflow
Solution 1 - Rubysepp2kView Answer on Stackoverflow
Solution 2 - RubyFelipeCView Answer on Stackoverflow
Solution 3 - RubyMBOView Answer on Stackoverflow
Solution 4 - RubyjohannesView Answer on Stackoverflow
Solution 5 - RubytroelsknView Answer on Stackoverflow
Solution 6 - RubyRameshView Answer on Stackoverflow
Solution 7 - RubyshackerView Answer on Stackoverflow
Solution 8 - RubyHomeTurfView Answer on Stackoverflow
Solution 9 - RubyruvanView Answer on Stackoverflow