drag-handle-corner-line
Jerod Santo

Jerod Santo

Buy the truth, sell it not*
🧐 About 📡 Subscribe 💼 Business 💬 Contact 🖇️ 𝕏

Traversing Directories with Ruby

If you want to shove filenames of all files in a directory into an array, do:

# (absolute path)
files = Dir["/Users/jerod/src/**"]
# (relative path)
files = Dir[File.expand_path("~/src") + "/**"]
# (in ENV["PWD"], aka current directory)
files = Dir["**"]

If you want to shove filenames of all files in a directory recursively into an array, do:

# (absolute path)
files = Dir["/Users/jerod/src/**/**"]
# (relative path)
files = Dir[File.expand_path("~/src") + "/**/**"]
# (in ENV["PWD"], aka current directory)
files = Dir["**/**"]

It doesn’t get much easier than that.