Search code examples
bashrenameleading-zero

(bash) Removing leading zeroes with a "v" right before said zeroes


First of all, I'm unable to use the "find" function, due to the files being .cbz, and recursing two layers deep would break them, thus, I'm left with only being able to recurse through one layer -- which is fine for my needs. I'm trying to do this on a mass scale with up to 1000 files being involved.

Currently:

Manga Name Here v004 - A Great Journey
Manga Name Here v053 - A Greater Journey
Manga Name Here v105 - The Best Journey

Expected

Manga Name Here, Volume 4 - A Great Journey
Manga Name Here, Volume 53 - A Greater Journey
Manga Name Here, Volume 105 - The Best Journey

I've attempted several methods, but.. I couldn't figure it out on my own. The main one that I'm trying that's failing is this.

rename 's/ v0([1-9]+)/, Volume $1/e' *v*.cbz

I don't quite get what I'm doing wrong, and I've tried more than "rename", but.. I couldn't figure out how to actually rename it to that, even if I got the leading zeroes gone. I appreciate any answers. Thank you in advanced!


Solution

  • Using Perl's rename (usable in any OS):

    rename -n 's/v(\d+)/sprintf "Volume %.1d", $1/e' ./*
    

    remove -n when happy with the output.

    Processed files:

    'Manga Name Here Volume 105 - The Best Journey'
    'Manga Name Here Volume 4 - A Great Journey'
    'Manga Name Here Volume 53 - A Greater Journey'