1

I'm running into an issue where I have a directory of files names:

something1.exr
something2.exr
something3.exr

and I need them named like

projectname.0000001.exr
projectname.0000002.exr
projectname.0000003.exr

I've come up with this:

NAME=image_test_GAM_4778x1806 c=1; for i in *.exr; do mv "$i" `printf $NAME."$c".exr`; let c=c+1; done

and it is able to rename files like:

image_test_GAM_4778x1806.1.exr
image_test_GAM_4778x1806.2.exr
image_test_GAM_4778x1806.3.exr

However, I need the incremented number to have 7-digit padded zeros so I found I can do that with "%07d", therefore I assume this code should work:

NAME=image_test_GAM_4778x1806 c=1; 
for i in *.exr; do 
    mv $i $(printf “%s.%07d.exr” “$NAME” “$c”); 
    let c=c+1; 
done

But it doesn't work and complains I'm using mv incorrectly. I know theres something wrong but logically it should make work, I'm trying to pass $NAME and $c to “%s.%07d.exr” respectively.

Can someone point me in the right direction? Any help would be appreciated.

Thank you.

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

Using rename utility with a perl expression:

rename -n "s/something(\d+)(.*)/'projectname.' . sprintf('%07d', \$1) . \$2/ge" *.exr

Using -n flag is useful to test until you found the exact expression:

something10.exr renamed as projectname.0000010.exr
something1.exr renamed as projectname.0000001.exr
something2.exr renamed as projectname.0000002.exr
something3.exr renamed as projectname.0000003.exr
Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
-1

Thanks to Barmar's suggestion in the comments - this was an issue with curly quotes.

I changed the quotes to regular parallel quotes and it works.

Rob
  • 26,989
  • 16
  • 82
  • 98