I'm trying to use perl-rename on Arch Linux to rename many files according to a regex. I have a large amount of files in a folder that follow this format:

Title of File [Year] - quality.filetype

where "Year" is 4 digits, quality could be any of 720p, 1080p, DVD, etc., and filetype can be any of mp4, mkv, etc.

I'm trying to rename the files to follow this format:

[Year] Title of File (quality).filetype

I've developed this regular expression to match the relevant parts of the file:

/(.+) \[(\d{4})\](?: - (.+)\.+)*/

After doing some research I developed this command to try and do the rename (I'll remove -n once I know it will work):

perl-rename -n 's/(.+) \[(\d{4})\](?: - (.+)\.+)*/[$2] $1 ($3)./'

When I run this command, perl-rename outputs nothing and doesn't exit. Can anyone help me spot the mistake?

closed as off-topic by Satō Katsura, Stephen Rauch, Kusalananda, xhienne, Stephen Kitt Sep 9 '17 at 21:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Satō Katsura, Stephen Rauch, Kusalananda, xhienne, Stephen Kitt
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You don't appear to be giving it any filenames to work on? – steeldriver Sep 9 '17 at 0:52
  • Thanks! As soon as I read your comment I realized my mistake. Adding the asterisk prevents the command from hanging. Now just to figure out why the string replacement isn't working... – Kev Sep 9 '17 at 0:57
  • Why are you saying \.+?  That means one or more periods — when do you need to match that? Why are you putting an asterisk after the entire ⁠ - quality subexpression?  Do you expect to match that more than once? – Scott Sep 9 '17 at 2:04

I wasn't specifying which files to rename. Added an asterisk to end of command:

perl-rename -n 's/(.+) \[(\d{4})\](?: - (.+)\.+)*/[$2] $1 ($3)./' *

Further, the way I was referencing capture groups was incorrect. Referencing capture groups is done like \1, \2, etc. rather than $1, $2, etc. The working expression looked like:

perl-rename -n 's/(.+) \[(\d{4})\](?: - (.+)\.+)*/[\2] \1 (\3)./' *

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