script for renaming a batch of files

hi i have a folder full of files. some of the names are quite off because the dimensions were the same and i had to put a 'b' after the initial number so that it didnt overwrite. what i want is a script in unix to overwrite the filwe name leaving some of the title intact, e.g. below are some filenames..
obj2-68x130x114.bin
obj1-46x48x39.bin
obj1b-25x33x39.bin
obj1-35x36x23.bin
what i would like is the file names to be rewritten leaving the bold part intact and the italics part to go down in chronological order from obj1 to objx, x being the final value in the series. it doesnt have to be by the modified date as this doesnt mater. i just need distinguishable filenames. thanks

I can think of a 2-step procedure that should work, if I understand:

n=1
for f in $(ls -tr obj*x*.bin); do
    mv -v "$f" "TEMP$n-${f#obj*-}"
    ((n++))
done
for f in TEMP*.bin; do
    mv -v "$f" "obj${f#TEMP}"
done
1 Like

Another approach. To prevent overwriting of files, move the renamed files in a temporary directory first:

ls obj*.bin | awk -F- '{system("mv " $0 " /temp/dir/obj" ++c FS $2)}'

Move them back:

mv /temp/dir/obj*.bin ./

*edit* it works, pasting error. thanks :slight_smile: