Reputation: 99
How would I renumber files in a directory like "ab0001.xyz" "ab0002.xyz" ... "ab0200.xyz" to the same numbering but adding a constant (say 97), so "ab0098.xyz" "ab0099.xyz"... "ab0297.xyz"
Also if you could add just a bit of commentary on each function so I don't have to ask again for something similar.
Thanks!
Upvotes: 0
Views: 215
Reputation: 29021
The solution depends on several things. For example, if you have always the same names and always are going to have four digit numbers, the solution is somewhat easier. For example:
CONSTANT=97
for i in `echo ab*.xyz | sort -r` ; do
filenumber=`echo $i | tr -dc 0-9`
numberadd=`expr $filenumber + $CONSTANT`
mv $i ab`printf "%04d" $numberadd`.xyz
done
You've got the CONSTANT
defined, then you extract just the numbers in the name and use them to sum it with the constant with expr
. Then, you change the name of the file using printf
to print just 4 digits filled with zeros.
Upvotes: 1
Reputation: 7028
You could use this script. Execute in the same directory where files are located. I'm sure there are more concise solutions...
#/usr/bin/env bash
export BASN=ab # basename
export EXT=xyz # extension
export CONST=97 # constant to add
for fl in ${BASN}*.${EXT}; do
noe=${fl%%.${EXT}} # trim extension
num=${noe##${BASN}} # trim basename
tar=${BASN}`printf '%04d' $((num + ${CONST}))`.${EXT} # construct target
mv ${fl} ${tar}
done
Upvotes: 1