# Find all files named "findthis.csv" in nested subfolders of "rootfolder"

**URL:** <https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096>\
**Category:** General Usage\
**Tags:** filesystem\
**Created:** [August 12, 2024, 1:59pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096 "2024-08-12T13:59:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 12, 2024, 1:59pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/1 "2024-08-12T13:59:37Z")

</div>

I am trying to do what the title says and collect the file paths into a vector but having trouble. I would use `find rootfolder -name findthis.csv` in bash. `readdir` can only read one level deep, so I am trying to find a solution with `walkdir`. I am not understanding the provided examples. Why is the same command run sequentially yielding different results?

> ```julia-repl
> julia> mkpath("my/test/dir");
> 
> julia> itr = walkdir("my");
> 
> julia> (root, dirs, files) = first(itr)
> ("my", ["test"], String[])
> 
> julia> (root, dirs, files) = first(itr)
> ("my/test", ["dir"], String[])
> 
> julia> (root, dirs, files) = first(itr)
> ("my/test/dir", String[], String[])
> 
> ```

```julia-repl
julia> itr = 1:4
1:4

julia> first(itr)
1

julia> first(itr)
1

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 12, 2024, 2:03pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/2 "2024-08-12T14:03:51Z")

</div>

`walkdir` returns a `Channel` as the iterator which is a mutable iterator. The docstring should probably mention this.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 12, 2024, 2:09pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/4 "2024-08-12T14:09:30Z")

</div>

If something is being mutated, then I would expect to see that indicated by the function: `first!`

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 12, 2024, 2:10pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/5 "2024-08-12T14:10:49Z")

</div>

I did see that but hoped there might be something simpler if I already know the exact file name.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 12, 2024, 2:11pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/6 "2024-08-12T14:11:40Z")

</div>

It’s just how a `Channel` works:

```julia
julia> chnl = Channel() do ch
                    foreach(i -> put!(ch, i), 1:5)
                end
Channel{Any}(0) (1 item available)

julia> first(chnl)
1

julia> first(chnl)
2

julia> first(chnl)
3

julia> first(chnl)
4

```

The docstring for `walkdir` just fails to warn about it being implemented as a `Channel`. Could be a good issue.

---

<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:** [August 12, 2024, 3:06pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/7 "2024-08-12T15:06:56Z")

</div>

Not an answer to why but might take you some of the way to a solution of the title question.

```julia
[root for (root, dirs, files) in walkdir(".") if filename in files]

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 12, 2024, 4:02pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/8 "2024-08-12T16:02:55Z")

</div>

PR for the docstring created here: [Improve `walkdir` docstring by nathanrboyer · Pull Request #55476 · JuliaLang/julia (github.com)](https://github.com/JuliaLang/julia/pull/55476)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 12, 2024, 5:02pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/9 "2024-08-12T17:02:38Z")

</div>

Please check this code adapted from @GunnarFarneback’s snippet above:

```julia
findfile(directory, file) = [joinpath(root, file) for (root, dirs, files) in walkdir(directory) if file in files]

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 12, 2024, 5:40pm UTC](https://discourse.julialang.org/t/find-all-files-named-findthis-csv-in-nested-subfolders-of-rootfolder/118096/10 "2024-08-12T17:40:47Z")

</div>

It looks like I/O objects may be mutated without indicating such with `!`. Though again, I wouldn’t necessarily realize I was creating an I/O object just to read some directory names, so it still strikes me as odd that `first` continues iterating the object.

> [@Why is it \`close(::Channel)\` instead of \`close!(::Channel)\`?](https://discourse.julialang.org/t/why-is-it-close-channel-instead-of-close-channel/24261/2):
>
> There is no explicit data structure being mutated. Yes, I/O operations change the state of the I/O stream but that’s inherent to the way they function. It could be viewed the other way but then all I/O operations would end in ! which seems unnecessary.
