Recursively List Files in Elixir

- 1 min

Quick Elixir ditty to recursively list the files at a given path (relative or absolute):

defmodule FileExt do
  def ls_r(path \\ ".") do
    cond do
      File.regular?(path) -> [path]
      File.dir?(path) ->
        File.ls!(path)
        |> Enum.map(&Path.join(path, &1))
        |> Enum.map(&ls_r/1)
        |> Enum.concat
      true -> []
    end
  end
end
FileExt.ls_r("../exgen")
["../exgen/.git/COMMIT_EDITMSG", "../exgen/.git/config",
 "../exgen/.git/description", "../exgen/.git/HEAD",
 "../exgen/.git/hooks/applypatch-msg.sample",
 "..."]

 FileExt.ls_r("/Users/ryan/projects/exgen")
 ["/Users/ryan/projects/exgen/.git/COMMIT_EDITMSG",
  "/Users/ryan/projects/exgen/.git/config",
  "/Users/ryan/projects/exgen/.git/description",
  "/Users/ryan/projects/exgen/.git/HEAD",
  "/Users/ryan/projects/exgen/.git/hooks/applypatch-msg.sample",
  "..."]
Ryan Daigle

Ryan Daigle

Director of Engineering at Spreedly. Half-stack developer. My bits are handcrafted, locally-sourced, and humanely raised.

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora