1

I have a directory of files

$ ls -1
ep2.mp4
ep3.mp4
ep4.mp4
ep5.mp4
ep6.mp4
ep7.mp4

I want to use the perl rename utility to make all files look like e.g.

ep02.mp4 

Command I am using

rename -n "s/([0-9])/0${1}/" *.mp4

current result

ep2.mp4 renamed as ep0.mp4
ep3.mp4 renamed as ep0.mp4
ep4.mp4 renamed as ep0.mp4
ep5.mp4 renamed as ep0.mp4
ep6.mp4 renamed as ep0.mp4
ep7.mp4 renamed as ep0.mp4

How can I capture the original digit and insert into the new filename?

  • You might want to consider something like 's/([0-9])/sprintf "%02d",$1/e' as a more general way to zero-pad the digit to the desired width – steeldriver Dec 13 '15 at 3:23
  • Im guess thats more POSIX - which I'm all for - but why would that rather complicated statement be better than a plain 0? – the_velour_fog Dec 13 '15 at 3:26
2

Use single quotes (or escape with \) around the replacement statement, otherwise shell will treat ${1} as variable.

So you can do:

rename -n 's/([0-9])/0${1}/' *.mp4

Or

rename -n "s/([0-9])/0\${1}/" *.mp4
  • 1
    ah yes of course - it works :) – the_velour_fog Dec 13 '15 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.