Need a REGEX to increment the file number of a pdf file

Hello,

I have a few thousand .pdf files in various folders each have a naming scheme like this:

006_-_Titled_Document_#34_-_September-25-2011-side-1.pdf

In each folder, the number system starts at 001 (as you see on the far left of the file name), and then ends at 999 (maximum .pdf files).

Somewhere in the collection of files say .pdf # 286, I have 286 twice (duplicate). Which screws up my numbering system.

I need a REGEX that I can enter into the shell when I'm in the .pdf directory, and start from say the duplicate # 286 and increment that 2nd duplicate # 286 & all the numbers after that by +1. So that they are all renamed appropriately.

This way I don't have to go in there and rename each .pdf file manually one by one.

Unfortunately, I am not very good with REGEX and haven't had much need for it in the past until now.

Would anyone know the best way to automate this .pdf renumbering task? I would appreciate any constructive thoughts on how to accomplish this.

Thank you.

Well, not regex, just programming in script. I assume the files are in two subtrees or have some name change, and there is a way you want them ordered, so we know who wins on collision? You could name them with temporary fractions with a decimal point and then list them, number them and rename them if they are not name right, starting from the top pulling them up, so there are no collisions.

The following script seems to do what you want, but no regular expressions are involved:

#!/bin/ksh
low=286
low_found=0
ls [0-9][0-9][0-9]_*.pdf|sort -r|while [ $low_found -eq 0 ] && IFS="" read -r fn
do      n=${fn%%_*}
        if [ $n -eq $low ]
        then    low_found=1
        fi
        if [ $n -lt $low ]
        then    break
        fi
        echo mv "$fn" $((n + 1))"${fn#???}"
done

I use (and tested this using) the Korn shell but this will work with any shell that recognizes the parameter expansions required by the POSIX standards. As written it will just list the mv commands that need to be run to do what you requested. If it does what you want remove the echo that appears before the mv command and it rerun it to actually rename the files.

Wouldn't

ls -r [0-9][0-9][0-9]_*.pdf

do the same? "ls" per default sorts alphabetically on file names, "-r" reverses the ordering, no?

I hope this helps.

bakunin

1 Like

Yes. Obviously. I have no idea why I didn't think of this when I was writing this script. :o

Thanks.