#!/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
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