# Rename files in a directory

**URL:** <https://discourse.julialang.org/t/rename-files-in-a-directory/84389>\
**Category:** New to Julia\
**Tags:** filesystem\
**Created:** [July 18, 2022, 8:55am UTC](https://discourse.julialang.org/t/rename-files-in-a-directory/84389 "2022-07-18T08:55:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 18, 2022, 8:55am UTC](https://discourse.julialang.org/t/rename-files-in-a-directory/84389/1 "2022-07-18T08:55:25Z")

</div>

In my directory, I have many files whose names all include the string “May\_2022”. For example “Bob\_May\_2022”. I want to change the name of every file so that the part “May\_2022” is replaced by “05\_22”. For example to “Bob\_05\_22”.

How can I do this using Julia code?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 18, 2022, 9:03am UTC](https://discourse.julialang.org/t/rename-files-in-a-directory/84389/2 "2022-07-18T09:03:34Z")

</div>

Should be possible with the commands `mv` and `walkdir` from `Base.Filesystem`.

Something like

```julia
# untested code!
for (root, dirs, files) in walkdir(pwd())  
    for fn in files
        if occursin("May_2022", fn)
            mv(joinpath(root,fn), joinpath(root, replace(fn, "May_2022" => "05_22"))) 
        end
    end
end

```

This goes recursively through your files and does the job. For sure there is a more elegant solution 😃

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 18, 2022, 9:26am UTC](https://discourse.julialang.org/t/rename-files-in-a-directory/84389/3 "2022-07-18T09:26:13Z")

</div>

That worked!
