user3447273 user3447273 - 11 days ago 7
Bash Question

Conditional replacement of character in filename

I am trying to rename:

01.black_cat.mp3
to
01_black_cat.mp3


But when I run:

rename -n 'y/^[0-9][0-9][.]*/^[0-9][0-9][_]*.mp3/' *.mp3


I get:

01.black_cat.mp3, 01_black_cat_mp3


I want to replace the first period but ignore the second one. What am I doing wrong? Can this be done easier with MMV? All of the examples I find are pattern matching for the end of the file or all instances within the filename.

Answer

I don't understand why you are using y/// instead of s///. Also, you need to escape . in the pattern, and the expression can be written simpler:

rename -n 's/^([0-9][0-9])\./$1_/' *mp3