Mass renaming in Bash based on regular expression

Written by - 0 comments

Published on August 19th 2019 - Listed in Bash Linux


If you want to rename multiple files according to a regular expression, this command could become a pretty long one-liner including a for loop and a lot of awk's. However there's an easier tool at hand: rename.

Let's use this list of files as the base line:

Paw Patrol S04-01 Pups Save a Blimp-Pups.mp4
Paw Patrol S04-02 Pups Save a Teeny.mp4
Paw Patrol S04-03 Pups Save a Playful.mp4
Paw Patrol S04-04 (Mission Paw) Quest for.mp4
Paw Patrol S04-05 Pups Save a Sleepover-Pups.mp4
Paw Patrol S04-06 Pups Save Jake's Cake-Pups.mp4
Paw Patrol S04-07 (Mission Paw) Royally Spooked-Pups.mp4
Paw Patrol S04-08 Pups Save the Flying.mp4
Paw Patrol S04-09 Pups Save a Sleepwalking.mp4
Paw Patrol S04-10 (Mission Paw) Pups Save.mp4
Paw Patrol S04-11 Pups Save Big Hairy-Pups.mp4
Paw Patrol S04-12 Pups Party with Bats-Pups.mp4
Paw Patrol S04-13 (Sea Patrol) Pups Save.mp4
Paw Patrol S04-14 Pups Save the Runaway.mp4
Paw Patrol S04-15 Pups Chill Out-Pups Save.mp4
Paw Patrol S04-16 (Sea Patrol) Pups save.mp4
Paw Patrol S04-17 Pups Save a Space.mp4
Paw Patrol S04-18 Pups Save a City.mp4
Paw Patrol S04-19 Pirate Pups to the.mp4
Paw Patrol S04-20 Pups Save the Mail-Pups.mp4
Paw Patrol S04-21 Pups Save the Runaway.mp4
Paw Patrol S04-22 Pups save a Frozen.mp4
Paw Patrol S04-23 Pups Save Luke Stars-Pups.mp4
Paw Patrol S04-24 Pups Save Francois the.mp4
Paw Patrol S04-25 Pups Save Baby Humdinger-Pups.mp4
Paw Patrol S04-26 (Sea Patrol) Pups Save.mp4

Goal: Rename all the S04-NN episode numbering to S04ENN. Obviously doing mv would involve a lot of parsing.

rename is a Perl script which can be installed with apt:

admck@WM2856l ~ $ sudo apt-get install rename

It allows the usage of regular expressions. In this case this means we have a fixed "S04" followed by a dash and a double digit. Which leads to the following command:

admck@WM2856l ~ $ rename 's/S04-(\d+)/S04E$1/' *.mp4

Note: If you're new to regular expressions: The parentheses allow to select the content (here the double digits after the dash) and paste it in later use as variable $1.

This results in the following new file list:

Paw Patrol S04E01 Pups Save a Blimp-Pups.mp4
Paw Patrol S04E02 Pups Save a Teeny.mp4
Paw Patrol S04E03 Pups Save a Playful.mp4
Paw Patrol S04E04 (Mission Paw) Quest for.mp4
Paw Patrol S04E05 Pups Save a Sleepover-Pups.mp4
Paw Patrol S04E06 Pups Save Jake's Cake-Pups.mp4
Paw Patrol S04E07 (Mission Paw) Royally Spooked-Pups.mp4
Paw Patrol S04E08 Pups Save the Flying.mp4
Paw Patrol S04E09 Pups Save a Sleepwalking.mp4
Paw Patrol S04E10 (Mission Paw) Pups Save.mp4
Paw Patrol S04E11 Pups Save Big Hairy-Pups.mp4
Paw Patrol S04E12 Pups Party with Bats-Pups.mp4
Paw Patrol S04E13 (Sea Patrol) Pups Save.mp4
Paw Patrol S04E14 Pups Save the Runaway.mp4
Paw Patrol S04E15 Pups Chill Out-Pups Save.mp4
Paw Patrol S04E16 (Sea Patrol) Pups save.mp4
Paw Patrol S04E17 Pups Save a Space.mp4
Paw Patrol S04E18 Pups Save a City.mp4
Paw Patrol S04E19 Pirate Pups to the.mp4
Paw Patrol S04E20 Pups Save the Mail-Pups.mp4
Paw Patrol S04E21 Pups Save the Runaway.mp4
Paw Patrol S04E22 Pups save a Frozen.mp4
Paw Patrol S04E23 Pups Save Luke Stars-Pups.mp4
Paw Patrol S04E24 Pups Save Francois the.mp4
Paw Patrol S04E25 Pups Save Baby Humdinger-Pups.mp4
Paw Patrol S04E26 (Sea Patrol) Pups Save.mp4

That's definitely much quicker!


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.