0

I have some TV shows stored on my Ubuntu server and I could rename them all, so Kodi can recognise them, exept one:

First Episode, first Season:

Name - S01E001 - Title part1
Name - S01E001 - Title part2
Name - S01E001 - Title part3

First Episode, second Season:

Name - S02E020 - Title part1
Name - S02E020 - Title part2
Name - S02E020 - Title part3

So Kodi thinks the second video is S02E20, but it is S02E01. The six files should be after the renaming:

First Episode, first Season:

Name - S01E001.1 - Title
Name - S01E001.2 - Title
Name - S01E001.3 - Title

First Episode, second Season:

Name - S02E001.1 - Title
Name - S02E001.2 - Title
Name - S02E001.3 - Title

I know, that there are many related questions here, but none fits this pattern and nearly noone explains his solution. The manpage of rename doesn't help me

1

The regular expression match /^(.*) - (.*) - (.*) part([0-9]+)/ stuffs the name, episode indicator, title and part number in groups which you can then use as $1 through $4 in the replacement text.

rename 's/^(.*) - (.*) - (.*) part([0-9]+)/$1 - $2.$4 - $3/' *

Anything after the part1 part, such as a file extension, is left unchanged.

If you want to modify the capture groups, use the e modifier at the end of the s/…/…/ expression. In s/REGEX/REPLACEMENT/e, the REPLACEMENT part is a Perl expression, not a string with variable interpolation. In a Perl expression, use the . operator to concatenate strings. For example, if you want to subtract 1 from the part number:

rename 's/^(.*) - (.*) - (.*) part([0-9]+)/"$1 - $2." . ($4-1) . " - $3"/e' *
2
  • Great answer, but one thing is missing. I have modified the regex, but need some help: ` rename -n 's/^(.*) - (S02)(E)([0-9]+) - (.*) part([0-9]+)/$1 - $2$3$4.$6 - $5/' *` does the same. Now I have to lower $4. How can I do this? The Bash way ($((42-21))) is not working
    – Motte001
    Jun 19 '16 at 9:03
  • @Motte001 See my edit. Jun 19 '16 at 18:19

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.