3

I'm new to regular expression, and am trying to use regex to rename some files. I am using rename (this one) to rename said files. Below is an example command.

$ rename -n 's/\(feat.//' *.mp3 (-n option = dry run)

Output:

'103 Flume - Left Alone (feat. Jezzabell Doran).mp3' 
would be renamed to '103 Flume - Left Alone  Jezzabell Doran).mp3'

'214 Flume - Sleepless (feat. Jezzabell Doran) (Shlohmo Remix).mp3' 
would be renamed to '214 Flume - Sleepless  Jezzabell Doran) (Shlohmo Remix).mp3'

What I'm trying to do is remove "(feat. artist_name)" from the filenames. (From the opening parenthesis to closing parenthesis). But I do not want to remove any other existence of parentheses from the filenames. For example, rename "214 Flume - Sleepless (feat. Jezzabell Doran) (Shlohmo Remix).mp3" to "214 Flume - Sleepless (Shlohmo Remix).mp3"

(Edit: Thomas gave an answer pertaining to what is below)
I know to remove the " - " in the filenames to do 's/ - //' but how could I do this in the same command as rename 's/\(feat.//' *.mp3 instead of doing rename 's/ - //' *.mp3 as a separate command

2 Answers 2

2

The command is like sed, which allows you to specify multiple substitutions using a -e option, e.g.,

rename -n -e 's/\(feat.//' -e 's/ - //' *.mp3

and get

'103 Flume - Left Alone (feat. Jezzabell Doran).mp3' would be renamed to '103 FlumeLeft Alone  Jezzabell Doran).mp3'                                            
'214 Flume - Sleepless (feat. Jezzabell Doran) (Shlohmo Remix).mp3' would be renamed to '214 FlumeSleepless  Jezzabell Doran) (Shlohmo Remix).mp3'

In the documentation, it says

-e, --expr

The code argument to this option should be a Perl expression that assumes the filename in the $_ variable and modifies it for the filenames to be renamed. When no other -c, -C, -e, -s, or -z options are given, you can omit the -e from infront of the code.

3
  • Ah, I did not even try that. Thank you!
    – cryptic
    May 28, 2016 at 0:22
  • no problem (if it's what you needed, you might mark it accepted). May 28, 2016 at 0:25
  • 1
    +1. you don't even need to use multiple -e options. any number of perl expression(s) can be used in -e, separated by semi-colons (;). e.g. combine both of the above into one expression rename -n -e 's/ *\(feat\..*?\) */ /;s/ - //' *.mp3
    – cas
    May 28, 2016 at 6:57
1

What you seem to want is a non-greedy match up to and including a closing parenthesis. You should also escape the period if you want it to be treated as literal:

$ rename -n 's/\(feat\..*?\)//' *.mp3
103 Flume - Left Alone (feat. Jezzabell Doran).mp3 renamed as 103 Flume - Left Alone .mp3
214 Flume - Sleepless (feat. Jezzabell Doran) (Shlohmo Remix).mp3 renamed as 214 Flume - Sleepless  (Shlohmo Remix).mp3
2
  • This works like a charm. Thanks for the help.
    – cryptic
    May 28, 2016 at 0:41
  • 1
    +1. can also get rid of the extra spaces left behind with s/ *\(feat\..*?\) */ /
    – cas
    May 28, 2016 at 6:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .