Bulk rename tool based on regular expressions to rename multiple files at once.
Quickstart
Renaming multiple files at once:
$ ls # awful names:
b45XDS-01.mp3 QsEW2s-02.mp3 VF7t6L-03.mp3
$ regex-rename '-(d+).mp3' '1_NeverGonnaGiveYouUp.mp3' --rename
[2022-04-01 09:19:15] DEBUG matching regex pattern pattern=-(d+).mp3 replacement=1_NeverGonnaGiveYouUp.mp3 full_match=False padding=None testing_mode=False
[2022-04-01 09:19:15] INFO renaming file from=QsEW2s-02.mp3 to=02_NeverGonnaGiveYouUp.mp3
[2022-04-01 09:19:15] INFO renaming file from=VF7t6L-03.mp3 to=03_NeverGonnaGiveYouUp.mp3
[2022-04-01 09:19:15] INFO renaming file from=b45XDS-01.mp3 to=01_NeverGonnaGiveYouUp.mp3
[2022-04-01 09:19:15] INFO files renamed count=3
$ ls # now we're talking:
01_NeverGonnaGiveYouUp.mp3 02_NeverGonnaGiveYouUp.mp3 03_NeverGonnaGiveYouUp.mp3
Installation
pip3 install regex-rename
It requires Python 3.7 (or newer) with pip.
Example
Imagine you’ve got audio files awfully named like this and you want to rename them:
Stanis▯aw+Lem+Invincible+(1).mp3
->01 The Invincible.mp3
Stanis▯aw+Lem+Invincible+(2 ).mp3
->02 The Invincible.mp3
Stanisław_Lem_Invincible (3) .mp3
->03 The Invincible.mp3
- …
Stanis▯aw+Lem+Invincible+(51).mp3
->51 The Invincible.mp3
Specifically, you want to extract the episode number, move it at the beginning,
and apply a 2-digit padding to it.
Step 1: Check the matching pattern
The Regex pattern to match these files and
extract episode number from parentheses may be as follows:
(d+).*mp3
(it contains a number and ends with mp3
)
Let’s check if the files are matched properly:
$ regex-rename '(d+).*mp3'
[2022-04-01 09:19:15] DEBUG matching regex pattern pattern=(d+).mp3 testing_mode=True
[2022-04-01 09:19:15] INFO matched file file="Stanisław_Lem_Invincible (3) .mp3" group_1=3
[2022-04-01 09:19:15] INFO matched file file="Stanis▯aw+Lem+Invincible+(1).mp3" group_1=1
[2022-04-01 09:19:15] INFO matched file file="Stanis▯aw+Lem+Invincible+(2 ).mp3" group_1=2
[2022-04-01 09:19:15] INFO matched file file="Stanis▯aw+Lem+Invincible+(51).mp3" group_1=51
[2022-04-01 09:19:15] INFO files matched count=4
Pay attention to the extracted regex groups.
Step 2: Check the replacement pattern
We’d like to replace all files to a pattern:
1 The Invincible.mp3
(1
is a first extracted group from matching pattern).
Regex can’t easily pad numbers with zeros.
Fortunately, we can use --pad-to=2
parameter to obtain 2-digit numbers.
Let’s test it by adding the replacement pattern:
$ regex-rename '(d+).*mp3' '1 The Invincible.mp3' --pad-to=2
[2022-04-01 09:19:15] DEBUG matching regex pattern pattern=(d+).mp3 replacement="1 The Invincible.mp3" testing_mode=True
[2022-04-01 09:19:15] INFO matched file
from="Stanisław_Lem_Invincible (3) .mp3" to="03 The Invincible.mp3" group_1=3
[2022-04-01 09:19:15] INFO matched file
from="Stanis▯aw+Lem+Invincible+(1).mp3" to="01 The Invincible.mp3" group_1=1
[2022-04-01 09:19:15] INFO matched file
from="Stanis▯aw+Lem+Invincible+(2 ).mp3" to="02 The Invincible.mp3" group_1=2
[2022-04-01 09:19:15] INFO matched file
from="Stanis▯aw+Lem+Invincible+(51).mp3" to="51 The Invincible.mp3" group_1=51
[2022-04-01 09:19:15] INFO files matched count=4
Step 3: Actual renaming
All above commands were just testing our patterns so that we could experiment with regex patterns.
Once we’re sure that everything is matched correctly, we can use --rename
flag,
which does the actual renaming:
$ regex-rename '(d+).*mp3' '1 The Invincible.mp3' --pad-to=2 --rename
[2022-04-01 09:19:15] DEBUG matching regex pattern pattern=(d+).mp3 replacement="1 The Invincible.mp3" testing_mode=False
[2022-04-01 09:19:15] INFO renaming file from="Stanisław_Lem_Invincible (3) .mp3" to="03 The Invincible.mp3"
[2022-04-01 09:19:15] INFO renaming file from="Stanis▯aw+Lem+Invincible+(1).mp3" to="01 The Invincible.mp3"
[2022-04-01 09:19:15] INFO renaming file from="Stanis▯aw+Lem+Invincible+(2 ).mp3" to="02 The Invincible.mp3"
[2022-04-01 09:19:15] INFO renaming file from="Stanis▯aw+Lem+Invincible+(51).mp3" to="51 The Invincible.mp3"
[2022-04-01 09:19:15] INFO files renamed count=4
Finally, files are named properly:
01 The Invincible.mp3
02 The Invincible.