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)

uguros
  • 167

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
uguros
  • 167
  • You wrote that in less than an hour or was this intended to be a self-answered question in the first place? – WinEunuuchs2Unix Jun 06 '20 at 20:24
  • @WinEunuuchs2Unix No! I was expecting an answer. Since there was no answer after ~40 minutes, I wrote a python code to solve my problem since it's an urgent task. I assume, you were going to tell me how to write a 'self answering question' but it's not a 'self answering question'. Thanks. – uguros Jun 06 '20 at 20:29