# How to list all subdirectories of a parent directory using Julia

**URL:** <https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331>\
**Category:** New to Julia\
**Created:** [June 3, 2021, 12:45pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331 "2021-06-03T12:45:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [June 3, 2021, 12:45pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/1 "2021-06-03T12:45:43Z")

</div>

I am looking to understand how to list all the subdirectories of a main directory using Julia. In shell script I would do something like this:

```julia
ls -d Simulations/SHmaxSv_*/*/ > dirNames.dat

```

I naively tried to do something like this, but it does not work:

```julia
julia> run(`ls -d Simulations/SHmaxSv_"*"/"*"/`)
ls: cannot access 'Simulations/SHmaxSv_*/*/': No such file or directory
ERROR: failed process: Process(`ls -d 'Simulations/SHmaxSv_*/*/'`, ProcessExited(2)) [2]

Stacktrace:
 [1] pipeline_error at ./process.jl:525 [inlined]
 [2] run(::Cmd; wait::Bool) at ./process.jl:440
 [3] run(::Cmd) at ./process.jl:438
 [4] top-level scope at none:1

```

let me know if anyone can help!

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [June 3, 2021, 12:53pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/2 "2021-06-03T12:53:16Z")

</div>

What is your current working directory? Use pwd()  
you might have to change directory to the place where Silulations is located

Also I would tend to use a find for this  
find Directory - type d

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [June 3, 2021, 1:06pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/3 "2021-06-03T13:06:51Z")

</div>

So there is `readdir`, which lists the files in a given folder.  
Combining it with `isdir` should work.

```julia
filter(isdir,readdir(path))

```

maybe you have to pass `join=true` to get the full path.  
If you want the filename back there is `basename`.

```julia
fulldirpaths=filter(isdir,readdir(path,join=true))
dirnames=basename.(fulldirpaths)

```

Further info:  
[https://docs.julialang.org/en/v1/base/file/](https://docs.julialang.org/en/v1/base/file/)

> **[GitHub - vtjnash/Glob.jl: Posix-compliant file name pattern matching](https://github.com/vtjnash/Glob.jl)**
>
> Posix-compliant file name pattern matching. Contribute to vtjnash/Glob.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 3, 2021, 1:37pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/4 "2021-06-03T13:37:08Z")

</div>

> [@josimar](#):
>
> I naively tried to do something like this, but it does not work:

That’s because the filename expansion is not done by `ls` but by your shell and Julia `Cmd` objects are intentionally not trying to mimic a shell. You can call the shell yourself though. Something like this should work:

```julia
readlines(`sh -c "ls -d Simulations/SHmaxSv_*/*/"`)

```

To find directories with Julia code, `readdir` and `walkdir` are your primary tools.

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [June 3, 2021, 1:53pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/5 "2021-06-03T13:53:46Z")

</div>

The solution proposed by @GunnarFarneback works. However, I am still curious how I would obtain the same results using `readdir` and `walkdir`. Could you comment on it?

Below is what I get when I use the proposed solution:

```julia
julia> lines = readlines(`sh -c "ls -d Simulations/SHmaxSv_*/*/"`)
70-element Array{String,1}:
 "Simulations/SHmaxSv_1.1/ShminSv_0.65/"
 "Simulations/SHmaxSv_1.1/ShminSv_0.82/"
 "Simulations/SHmaxSv_1.1/ShminSv_0.9/"
 "Simulations/SHmaxSv_1.1/ShminSv_1.07/"
 "Simulations/SHmaxSv_1.1/ShminSv_1.15/"
 "Simulations/SHmaxSv_1.1/ShminSv_1.23/"
 ....

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [June 3, 2021, 2:12pm UTC](https://discourse.julialang.org/t/how-to-list-all-subdirectories-of-a-parent-directory-using-julia/62331/6 "2021-06-03T14:12:57Z")

</div>

> [@josimar](#):
>
> However, I am still curious how I would obtain the same results using `readdir` and `walkdir` . Could you comment on it?

The `Base` functions don’t support glob matching, but you can use the `Glob` package:

```julia
using Glob
filter(isdir, readdir(glob"Simulations/SHmaxSv_*/*"))

```
