0

I have thousands of images. Their names is as follows:

img_1
img_2
img_3
...
img_36000

I would like to covert them into that (respectively):

img_010251
img_010252
img_010253
...
img_046250

Is there a quick solution for that in Ubuntu 18.04?

Note: The length of the numbering part must be 6! (NNNNNN)

ugur
  • 177

1 Answers1

1

I was looking for a solution as bash script.

However, I have just written a python code for that, and it worked:

# importing modules 
import os, sys, glob

# args
main_folder = sys.argv[1]
start_num = int(sys.argv[2])
extension = sys.argv[3]

len_removal = 6 + len(extension)

# importing names
src_path = main_folder + '*.' + extension
src_files=sorted(glob.glob(src_path))

for i in range(0,len(src_files)):
    newname = src_files[i][:-len_removal] + str(start_num).zfill(6) +"." + extension
    os.rename(src_files[i],newname)
    start_num+=1
ugur
  • 177