Multiple file rename using regular expression

I have a bunch of episode files in my directory that I would like to rename to proper S[XX]E[YY] format.

Current directory listing looks like this:

S1 Episode 10.avi S1 Episode 11.avi S1 Episode 12.avi S1 Episode 13.avi S1 Episode 14.avi S1 Episode 15.avi S1 Episode 16.avi S1 Episode 17.avi S1 Episode 18.avi S1 Episode 19.avi S1 Episode 1.avi S1 Episode 20.avi S1 Episode 21.avi S1 Episode 22.avi S1 Episode 23.avi S1 Episode 24.avi S1 Episode 25.avi S1 Episode 26.avi S1 Episode 27.avi S1 Episode 28.avi S1 Episode 2.avi S1 Episode 3.avi S1 Episode 4.avi S1 Episode 5.avi S1 Episode 6.avi S1 Episode 7.avi S1 Episode 8.avi S1 Episode 9.avi S2 Episode 10.avi S2 Episode 11.avi S2 Episode 12.avi S2 Episode 13.avi S2 Episode 14.avi S2 Episode 15.avi S2 Episode 16.avi S2 Episode 17.avi S2 Episode 18.avi S2 Episode 19.avi S2 Episode 1.avi S2 Episode 20.avi S2 Episode 21.avi S2 Episode 22.avi S2 Episode 23.avi S2 Episode 24.avi S2 Episode 2.avi S2 Episode 3.avi S2 Episode 4.avi S2 Episode 5.avi S2 Episode 6.avi S2 Episode 7.avi S2 Episode 8.avi S2 Episode 9.avi S3 Episode 10.avi S3 Episode 11.avi S3 Episode 12.avi S3 Episode 13.avi S3 Episode 14.avi S3 Episode 15.avi S3 Episode 16.avi S3 Episode 17.avi S3 Episode 18.avi S3 Episode 19.avi S3 Episode 1.avi S3 Episode 20.avi S3 Episode 21.avi S3 Episode 22.avi S3 Episode 23.avi S3 Episode 24.avi S3 Episode 2.avi S3 Episode 3.avi S3 Episode 4.avi S3 Episode 4.avi S3 Episode 6.avi S3 Episode 7.avi S3 Episode 8.avi S3 Episode 9.avi

I would like rename all *.avi files to following format S01E01.avi so that I would add leading zeroes for single digit episodes.

Easiest way is to do this in two steps with rename command which has cool option to do a dry run with -n switch.

First step is to replace double digit episodes, and here is test run:

rename -n -v 's/S([1-3])\ Episode\ (\d{2})/S0$1E$2/' *.avi

If it looks fine, you can execute this:

rename -v 's/S([1-3])\ Episode\ (\d{2})/S0$1E$2/' *.avi

Second step is to replace single digit episodes and to add leading zero to single digit episode number with test run:

rename -n -v 's/S([1-3])\ Episode\ (\d)/S0$1E0$2/' *.avi

If it looks fine, you can execute this:

rename -v 's/S([1-3])\ Episode\ (\d)/S0$1E0$2/' *.avi

Now the resulting directory has following content:

S01E01.avi S01E02.avi S01E03.avi S01E04.avi S01E05.avi S01E06.avi S01E07.avi S01E08.avi S01E09.avi S01E10.avi S01E11.avi S01E12.avi S01E13.avi S01E14.avi S01E15.avi S01E16.avi S01E17.avi S01E18.avi S01E19.avi S01E20.avi S01E21.avi S01E22.avi S01E23.avi S01E24.avi S01E25.avi S01E26.avi S01E27.avi S01E28.avi S02E01.avi S02E02.avi S02E03.avi S02E04.avi S02E05.avi S02E06.avi S02E07.avi S02E08.avi S02E09.avi S02E10.avi S02E11.avi S02E12.avi S02E13.avi S02E14.avi S02E15.avi S02E16.avi S02E17.avi S02E18.avi S02E19.avi S02E20.avi S02E21.avi S02E22.avi S02E23.avi S02E24.avi S03E01.avi S03E02.avi S03E03.avi S03E04.avi S03E04.avi S03E06.avi S03E07.avi S03E08.avi S03E09.avi S03E10.avi S03E11.avi S03E12.avi S03E13.avi S03E14.avi S03E15.avi S03E16.avi S03E17.avi S03E18.avi S03E19.avi S03E20.avi S03E21.avi S03E22.avi S03E23.avi S03E24.avi

There are some version of rename command that do not support Perl regular expressions, so if this does not work please check if it is supported:

#rename --help
 Unknown option: help
 Usage: rename [-v] [-n] [-f] perlexpr [filenames]

Leave a Reply

Your email address will not be published. Required fields are marked *