Recursively List Files in Elixir
- 1 minQuick 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",
"..."]