1

I'm trying to change files that look like

01 Track name.mp3

into

01 - Track name.mp3

So far my futile attempts were

rename '0. ' '$&- ' *.mp3
rename 's/0. /$&- /' *.mp3
rename '/0. /' '/$&- /' *.mp3
rename 's/0.\ /$&-\ /' *.mp3
rename 's/0.\ /$1-\ /' *.mp3
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • rename -n 's/ / - /' *.mp3 should do it. that will change only the first space to space-dash-space. test it first with -n and if it does what you want run it without -n. – cas Jun 21 '16 at 13:03

5 Answers5

2

Does it have to use the rename command?

$ ls
01 Track name.mp3  02 Track name.mp3  03 Track name.mp3

$ for a in *.mp3
> do
> mv -i "$a" "${a%% *} - ${a#* }"
> done

$ ls
01 - Track name.mp3  02 - Track name.mp3  03 - Track name.mp3
2

Assuming the perl rename command:

You're quite close with the last command. rename 's/(0.) /$1 - /' *.mp3 would work. There's no need to escape the space, they have no special meaning in regular expressions (they do in file names, but that doesn't matter here), and you need parentheses around the part you want to reuse.

  • I think my question was a bit misleading; although I didn't mean perl rename, that seems to be the easiest way so I installed it. Thanks! – Yousef Amar Jun 21 '16 at 13:11
1

That's the Perl rename, I suppose. Perhaps something like this would work:

rename 's/^(\d+) ([^-])/$1 - $2/' [0-9]*.mp3

Match anything starting with numbers, then a space, then something other than a dash. Replace with the numbers, a dash, and the next character. (The rest of the name is not touched.) Explicitly checking for the dash here so repeated applications don't end up with files like 01 - - Track name.mp3.

Actually, your second example seems to work, though of course for names where the first digit is a zero. We could change that to any digits and replace my second captured expression with a negative look-ahead to still aboud adding more than one dash.

rename 's/^\d+ (?!-)/$&- /' *.mp3

((?!pattern) will match a position that is not followed by the pattern, but the match is zero-width, so it doesn't cause a replacement.)

ilkkachu
  • 138,973
  • Doesn't work unfortunately, maybe because it's a different rename. The one I mean is the built in one (util-linux package). – Yousef Amar Jun 21 '16 at 13:00
  • 1
    ah, some of the examples were clearly Perl, so I went with that. You might have Perl rename as prename, though? – ilkkachu Jun 21 '16 at 13:05
  • I think my question was a bit misleading, but it seems that Perl rename is the easiest, so I installed it. Thanks for answering! – Yousef Amar Jun 21 '16 at 13:11
  • 1
    What is actually misleading is that there are two different commands called rename. :) But I think the util-linux rename only does fixed patterns, so it's a bit difficult to use here. Of course even without Perl, a simple sed command could be used (or the shell string expressions in Stephen's answer). – ilkkachu Jun 21 '16 at 13:16
1
$ rename 's/^(\d\d)\s*/$1 - /' *.mp3

This will rename all MP3 files that has a double digit at the start of their file names, inserting space-dash-space after the digits. So 01 Track name.mp3 will become 01 - Track name.mp3

Judging from your own attempts, all filenames start with the digit zero, and you appear to want to insert a dash directly after the digits followed by a space:

$ rename 's/^(0\d)\s*/$1- /' *.mp3

This transforms 01 Track name.mp3 to 01- Track name.mp3.

Kusalananda
  • 333,661
0

There are two (three) erase commands: one that's part of the util-linux package that's installed on every non-embedded Linux system, and one (two variants actually) based on Perl. See What's with all the renames: prename, rename, file-rename?

The util-linux command is very basic, but you're in the rare situation where it can do what you want. Replace the first space by space-dash-space:

rename ' ' ' - ' [0-9][0-9]\ [^-]*