# Removing directory contents recursively

**URL:** <https://discourse.julialang.org/t/removing-directory-contents-recursively/128761>\
**Category:** General Usage\
**Tags:** shell, filesystem\
**Created:** [May 6, 2025, 2:45pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761 "2025-05-06T14:45:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [May 6, 2025, 2:45pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/1 "2025-05-06T14:45:00Z")

</div>

Hi all, still struggling with this.  
I want to run command `rm -rf /path/to/dir/*`, so that I delete the content but not the dir itself. I get this

```julia
julia> Base.run(`rm -rf $(ReproData.root())/code/"*"`)
Process(`rm -rf '/Users/floswald/git/ReproWorkshop/code/*'`, ProcessExited(0))

```

but that does not work because this `'/Users/floswald/git/ReproWorkshop/code/*'` is different from `/Users/floswald/git/ReproWorkshop/code/*`. The problem is the star character:

```julia
julia> Base.run(`rm -rf $(ReproData.root())/code/*`)
ERROR: LoadError: parsing command `rm -rf $(ReproData.root())/code/*`: special characters "#{}()[]<>|&*?~;" must be quoted in commands

```

how to do this quoting properly?

thanks

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 6, 2025, 3:03pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/2 "2025-05-06T15:03:34Z")

</div>

> [@floswald](#):
>
> how to do this quoting properly?

It has nothing to do with quoting. As explained in the old thread you just resurrected, wildcards like `*` are a feature of a shell, not a feature of `rm`, so you need to execute a shell, e.g.

```julia
run(`sh -c "rm -rf $(ReproData.root())/code/*"`)

```

Of course, this is not portable because not all systems have `sh` or `rm`. You could also do it in Julia, something like:

```julia
foreach(readdir(joinpath(ReproData.root()), "code"), join=true)) do filename
    rm(filename, recursive=true, force=true)
end

```

or using the Glob.jl package for more flexibility.

PS. Note that using shell globbing like `*` has been discussed extensively. See also [Is there any way to run an external command with \* in it?](https://discourse.julialang.org/t/is-there-any-way-to-run-an-external-command-with-in-it/73926) and [How to use \* with backticks](https://discourse.julialang.org/t/how-to-use-with-backticks/11682) and [Delete a file from script using wildcards?](https://discourse.julialang.org/t/delete-a-file-from-script-using-wildcards/7022) and [Executing system commands "backtick-like" way in Julia?](https://discourse.julialang.org/t/executing-system-commands-backtick-like-way-in-julia/114890) … if an old thread does not clarify things for you, please **do not comment on an old thread with new questions**. Create a _new_ thread, explain your question, and feel free to link other threads that might be relevant.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [May 6, 2025, 3:05pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/3 "2025-05-06T15:05:49Z")

</div>

oh right! sorry I didn’t get the `bash` call there in front. thanks a lot!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 6, 2025, 3:58pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/5 "2025-05-06T15:58:21Z")

</div>

I don’t understand the need for an `IOBuffer` in that example. Why not just define a string with the desired script directly and pass it to `powershell`, rather than writing a string to a buffer and then converting back to a string?

---

<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:** [May 6, 2025, 4:14pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/7 "2025-05-06T16:14:59Z")

</div>

> [@stevengj](#):
>
> ```julia
> foreach(readdir(joinpath(ReproData.root()), "code"), join=true)) do filename
> rm(filename, recursive=true, force=true)
> end
> 
> ```

To remove everything under a directory `dir_path`, isn’t it enough to write in Julia:

```julia
rm(dir_path, recursive=true, force=true)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 6, 2025, 4:24pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/8 "2025-05-06T16:24:31Z")

</div>

> [@rafael.guerra](#):
>
> To remove everything under a directory `dir_path`, isn’t it enough to write in Julia:

That will remove the directory too, which the OP doesn’t want.

---

<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:** [May 6, 2025, 4:31pm UTC](https://discourse.julialang.org/t/removing-directory-contents-recursively/128761/9 "2025-05-06T16:31:50Z")

</div>

OK, thanks, even if that would be just a `mkdir(dir_path)` away.
