# Rename multiple files in the folder with Julia

**URL:** <https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976>\
**Category:** Data\
**Tags:** filesystem\
**Created:** [March 25, 2021, 10:35pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976 "2021-03-25T22:35:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 25, 2021, 10:35pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/1 "2021-03-25T22:35:15Z")

</div>

I am working with 800 txt files in the same directory. I would like to rename all these files as follows:

**1.txt**  
**2.txt**  
**…**  
**799.txt**  
**800.txt**

How do you think this would be possible with Julia?

Thanks in advance,  
Bruno

---

<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:** [March 25, 2021, 10:46pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/2 "2021-03-25T22:46:15Z")

</div>

You can get a list of file names using `readdir`. From there you can iterate over all the files and call `mv` to rename them.

```julia

files = readdir()
txt_files = filter(endswith(".txt"),files)
for (i,file) in enumerate(txt_files)
   mv(file,"$(i).txt")
end

```

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 25, 2021, 10:53pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/3 "2021-03-25T22:53:27Z")

</div>

Dear Michael,  
Thank you very much.  
Cheers  
Bruno

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 26, 2021, 2:38pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/4 "2021-03-26T14:38:21Z")

</div>

Dear Michael,  
I apologize for the many questions, but I had a problem. Taking into account the first 20 files of my list (with 800 txt files), the txt-files array is:

> Blockquote  
> [“I\_0,6\_0,2\_20\_10\_1.txt”, “I\_0,6\_0,2\_20\_10\_10.txt”, “I\_0,6\_0,2\_20\_10\_2.txt”, “I\_0,6\_0,2\_20\_10\_3.txt”, “I\_0,6\_0,2\_20\_10\_4.txt”, “I\_0,6\_0,2\_20\_10\_5.txt”, “I\_0,6\_0,2\_20\_10\_6.txt”, “I\_0,6\_0,2\_20\_10\_7.txt”, “I\_0,6\_0,2\_20\_10\_8.txt”, “I\_0,6\_0,2\_20\_10\_9.txt”, “I\_0,6\_0,2\_20\_5\_1.txt”, “I\_0,6\_0,2\_20\_5\_10.txt”, “I\_0,6\_0,2\_20\_5\_2.txt”, “I\_0,6\_0,2\_20\_5\_3.txt”, “I\_0,6\_0,2\_20\_5\_4.txt”, “I\_0,6\_0,2\_20\_5\_5.txt”, “I\_0,6\_0,2\_20\_5\_6.txt”, “I\_0,6\_0,2\_20\_5\_7.txt”, “I\_0,6\_0,2\_20\_5\_8.txt”, “I\_0,6\_0,2\_20\_5\_9.txt”]

> Blockquote

Unfortunately, the files are not in the order in which they appear in the folder. They are sorted taking into consideration the digits.

How could I use the files in the same order in which they appear in the directory? I tried several times without success.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 26, 2021, 2:43pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/5 "2021-03-26T14:43:40Z")

</div>

You can sort on whatever you like using `Base.stat` and the `by` argument to `sort`.

[https://docs.julialang.org/en/v1/base/file/#Base.stat](https://docs.julialang.org/en/v1/base/file/#Base.stat)  
[https://docs.julialang.org/en/v1/base/sort/#Base.sort](https://docs.julialang.org/en/v1/base/sort/#Base.sort)

For example:

```julia
sort(readdir("."); by=f->stat(f).inode)

```

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 26, 2021, 6:15pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/6 "2021-03-26T18:15:21Z")

</div>

Dear Jeff,  
Thanks a lot.  
Cheers,  
Bruno

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 26, 2021, 6:18pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/7 "2021-03-26T18:18:07Z")

</div>

I think you can also do readdir(dir, sort = false)

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [March 26, 2021, 6:37pm UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/8 "2021-03-26T18:37:28Z")

</div>

Thanks a lot, Jules. Cheers.

---

<div class="post-metadata">

**Author:** ![hillelawaskar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hillelawaskar/32/26276_2.png) [@hillelawaskar](https://discourse.julialang.org/u/hillelawaskar)\
**Post date:** [June 20, 2021, 10:40am UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/9 "2021-06-20T10:40:11Z")

</div>

# Jun20,2021 : Rename files in one single folder

```julia-auto
const BASEDIR="/home/hillel/workjulspace/testdata/B"
files = readdir(BASEDIR, join=true, sort = true)
filenames = readdir(BASEDIR,sort = true)
#println(files)
#println(filenames)
#println(files)
img_files = filter(endswith(".png"),files)
for (i,file) in enumerate(img_files)
    #println(i)
    #println(file)
    mv(file,"/home/hillel/workjulspace/testdata/B/BB_$(filenames[i])")
end

```

---

<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 20, 2021, 11:03am UTC](https://discourse.julialang.org/t/rename-multiple-files-in-the-folder-with-julia/57976/10 "2021-06-20T11:03:44Z")

</div>

I think this might not be what you want?  
By filtering the files array you also change its index.

```julia
using Logging
ENV["JULIA_DEBUG"] = Main

const BASEDIR="/home/hillel/workjulspace/testdata/B"

files = readdir(BASEDIR, join=true)
png_files = filter(endswith(".png"), files)
for file in png_files
   new_filename = join(["BB", basename(file)], "_")
   new_file = joinpath(BASEDIR, new_filename)
   @debug "moving file" file new_file
   mv(file, new_file)
end

```
