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

57 Views Asked by At

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!

3

There are 3 best solutions below

7
Gilles Quénot On BEST ANSWER

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'
0
Ed Morton On

It sounds like all you need is (untested):

#!/usr/bin/env bash

for old in *v*.cbz; do
    if [[ $old =~ (.* )v0*([1-9].*) ]]; then
        new="${BASH_REMATCH[0]}, Volume ${BASH_REMATCH[1]}"
        echo mv -- "$old" "$new"
    fi
done
0
pmf On

I couldn't figure out how to actually rename it to that, even if I got the leading zeroes gone.

For some reason, it's not working. I can't explain why, either. Like, by all logic and all that, it should, but it isn't.

If you happen to be using the rename tool that is part of the util-linux package from the Linux Kernel Archive (see https://www.kernel.org/pub/linux/utils/util-linux/), it has to be called as follows (see the source and the man page):

rename [options] expression replacement file...

Thus, calling this rename needs at least three arguments, and as expression and replacement are not treated as regex, for your "up to 1000 files" you'd need to call it up to three times, depending on how many 0s could be prefixing your numbers (the fourth case for number 000 can be integrated into any other case). The -n flag induces a test run with no changes made, and the -v flag shows which files it would rename to what. Remove -nv if the test run output looks promising:

n="Manga Name Here"
rename -nv "$n v00" "$n, Volume " "$n"\ v00[0-9]\ -*.cbz
rename -nv "$n v0"  "$n, Volume " "$n"\ v0[1-9][0-9]\ -*.cbz
rename -nv "$n v"   "$n, Volume " "$n"\ v[1-9][0-9][0-9]\ -*.cbz
`Manga Name Here v004 - A Great Journey.cbz' -> `Manga Name Here, Volume 4 - A Great Journey.cbz'
`Manga Name Here v053 - A Greater Journey.cbz' -> `Manga Name Here, Volume 53 - A Greater Journey.cbz'
`Manga Name Here v105 - The Best Journey.cbz' -> `Manga Name Here, Volume 105 - The Best Journey.cbz'

I'm unable to use the "find" function, due to the files being .cbz, and recursing two layers deep would break them

I don't quite understand why/how specifying the ending of a file name and/or facing a nested file tree would keep you from using find, but as far as this rename tool is concerned, let me hint you to the manual which further states:

if either expression or replacement contains a /, the full path is updated. This can cause a file to be moved between folders.