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
  • 2
  • 10
  • [Sequential renaming of files](https://askubuntu.com/questions/679283/sequential-renaming-of-files) does not solve my problem because my starting point is not 1 but 10251. What i need is to offset the names with a starting point (here 10521) and keep the length 6. – uguros Jun 06 '20 at 19:16
  • You are asking how to add `10520` to a filename and prefix it with one leading `0`? – WinEunuuchs2Unix Jun 06 '20 at 19:51
  • @WinEunuuchs2Unix no! I am not asking "... with one laeding 0". The length of the number must be 6 as I stated in the question (NNNNNN). – uguros Jun 06 '20 at 19:57
  • 4
    You can straightforwardly modify the expression from the linked answer to add an offset ex. `s/\d+/sprintf("%06d", $&+10250)/e` – steeldriver Jun 06 '20 at 20:18

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
  • 1
  • 2
  • 10
  • 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