• 0

[bash] Renaming a bunch of files


Question

Hey all, I've got a request for some help with a bash file, as I don't know where to start...

I have a bunch of files that end with ".SaaEbb.avi" (aa being two-digit number with a 0 at the beginning and bb being a two digit number). Example: Family.Guy.S05E13.avi, Prison.Break.S02E02.avi, Lost.S04E10.avi, etc

What i want to do is rename the files so that they end on ".axbb.avi" (so that the leading zero in a is removed and the format is different). Example: Family.Guy.5x13.avi, Prison.Break.2x02.avi, Lost.4x10.avi

All the season counters have a leading zero, so that may be helpful in making the simple script simpler!

I know I need to use "mv" to rename files, but I have no clue how to actually do some magic foo on the filenames to figure out what the new filename needs to be. Could someone please help me? Thanks!

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I don't know about Bash, since I've always just used Perl whenever I needed anything remotely complicated (and thus never bothered to learn much about Bash), but with Perl, this is extremely easy to do. In fact, you can even do it with a one-liner:

#!/usr/bin/perl
map { /(.+\.)S(\d+)E(\d+)(\..+)/ && rename($_, sprintf("%s%dx%02d%s",$1,$2,$3,$4)) } <*.avi>;

Edit: tweaked the code so that it also handles Stuff.SnnEnn.More.Stuff.avi.

Edited by code.kliu.org
Link to comment
Share on other sites

  • 0

There's also a command called rename where you can just do something like:

rename S0 S *S??E*

Where S0 is the part you want to rename, S is what to replace it with, and the third argument is the files.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.