Renumbering files bash script

I am new to the world of Linux scripting, and would like to make the following 2 scripts:

I have 67 files named Alk-0001.txt to Alk-0067.txt
I would like them to be numbered Alk-002.txt to Alk-0134.txt
eg

Alk-0001.txt > Alk-0002.txt
Alk-0002.txt > Alk-0004.txt
Alk-0003.txt > Alk-0006.txt

Basically just the original file number doubled.

I also have files that I would like renumbered such that the original file number is doubled then minus 1

eg

Alk-0001.txt > Alk-0001.txt
Alk-0002.txt > Alk-0003.txt
Alk-0003.txt > Alk-0005.txt

I imagine these scripts should be easy but I can't work it out at present, can anyone help?

Many thanks in advance,

Try

#!/bin/ksh

for i in *.txt; do
	
	[[ "$i" = */* ]] && path="${i%/*}" || path="."

	file="${i##*/}"
        base="${file%.*}"
	ext="${i##*.}"
	new=$(printf "%s/%s-%04d.%s" $path ${base%-*}  $((${base##*-}*2-1)) $ext)

	# remove echo cp if you feel result is as expected	
	echo cp $i $new

done

Usage

ksh yourscript

I am hoping you are going to be careful and create a backup before doing any test on it, otherwise you most likely are going to overwrite your files.

Look at this:

Alk-0001.txt > Alk-0002.txt
Alk-0002.txt > Alk-0004.txt
Alk-0003.txt > Alk-0006.txt

There's a file Alk-0002.txt already, right? What do you think it will happen if you
Alk-0001.txt > Alk-0002.txt
And then you
Alk-0002.txt > Alk-0004.txt ?
You will be practically overwriting files right and left.

Any solution presented must have that in consideration. I think it would have to be a two pass renaming in order to be safe.

Let me show you how to test safely

mkdir working_dir && cd working_dir
touch Alk-{0001..0067}.txt

Now, you have a dummy army of files to test.
cp the script to this file and execute it only when you are cd into.

Now a possible solution for the first request

#!/bin/ksh

change_serial() {

    for f in Alk-*.txt; do
        fnameplus=${f%.*}
        ext="${f##*.}"
        serial="${fnameplus##*-}"

        case "$1" in
            1)
                trim_serial=$(printf "%010d" "$((serial*2))");
            ;;
            2)
                trim_serial="${serial:6}"
            ;;
            *)
                echo "bailing out";
                exit 1;
            ;;
        esac

        mv "$f" "Alk-${trim_serial}.${ext}"
    done
}

change_serial 1
change_serial 2

Here is a slight change to Aia's code that uses a tmp folder instead of two passes.

It also accepts formula, format and filename wildcard:

change_serial() {
    mkdir ./tmp_$$

    fn=$1
    fmt=$2
    shift 2

    for f in $*
    do
        prefix=${f%%[0-9]*}
        suffix=${f##*[0-9]}
        serial=${f#$prefix}
        serial=10#${serial%$suffix}
        new=$(printf "$fmt" $(($fn)) )
        mv $f ./tmp_$$/$prefix$new$suffix
    done

    mv ./tmp_$$/* ./
    rmdir tmp_$$
}

change_serial 'serial*2'   '%04d' "Alk-*.txt"
change_serial 'serial*2-1' '%04d' "Blk-*.txt"