user3447273 user3447273 - 1 year ago 76
Bash Question

Conditional replacement of character in filename

I am trying to rename:


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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download