How do I batch renumber files in sequence?

I have a sequence of image files that look like this:

image-149454.jpg
image-149455.jpg

I have some other images that I want to append to the end of the sequence where it left off but currently they are numbered from 0 (i.e. image-000000 to image-010000).

What's a script I could use to rename those new images starting from a certain number and going on, in this case 149456 and onwards?

Answers


I would try something like this. You just have to adjust the offset and add a string in front of the name, just as you need it.

x=1
for i in *.jpg; do
    temp=$(printf "%08d.jpg" ${x}) #padding since you seem to want it
    mv ${i} ${temp}
    let x=x+1
done

Here's an untested solution in Python. This should change all of the files in a directory to have sequentially higher numbers than the other.

To use it enter: script.py dir1 dir2

Assuming the original files have the higher number (149455) are in dir1 and the new files start from 000000 are in dir2:

import os, sys, re

max_image = 0
# check if current (with higher numbers) and other (with lower) directory is given
if len(sys.argv) == 3:
    for files in os.listdir(sys.argv[1]): # first dir is current
        if files.endswith(".jpg"):
            #for all jpgs get the max
            m = re.search("\-(\d+)", files)
            number = int(m.group(1))
            if max_image < number:
                max_image = number

    for files in os.listdir(sys.argv[2]): # second dir is other
        if files.endswith(".jpg"):
            #get current start point
            m = re.search("\-(\d+)", files)
            number = int(m.group(1))
            os.rename(files, "image-" + str(number + max_image)+".jpg") # add the max from current folder

Here's a pure bash solution which uses the last image sequence as your starting point (per your request):

#!/bin/bash
last_seq=$(ls image-* | tail -1 | sort -n | cut -c7-) # grab the sequence number
last_seq=${last_seq%.jpg} # remove the trailing .jpg
if [ -z $last_seq ] ; then
  echo "Unable to obtain the last image sequence number"
  exit 1
fi
for image in image-0*.jpg ; do
   [ -f ${image} ] || break  # In case no files match image-0*.jpg
   let last_seq=last_seq+1
   mv -v ${image} image-$(printf "%06d.jpg" ${last_seq})
done

Here's how it worked locally:

before the run:

~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-00000.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-01000.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff   418 Dec 12 10:24 testh.sh

during the run:

~/tmp › sh ./testh.sh
image-00000.jpg -> image-14911.jpg
image-01000.jpg -> image-14912.jpg

after the run:

~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-14911.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-14912.jpg
-rw-r--r-- 1 dyoung staff   429 Dec 12 12:25 testh.sh

Need Your Help

i want to fetch all the values using regex in a single regcular expression extractor in jemter

regex jmeter

This is my input box. I want to fetch the value of this box in a single regular expression extractor.