Ubuntu – Batch rename with id shifting

batch-renamecommand line

I need to batch rename bunch of files with id in name, adding a constant number to id

 <id>-xxx.txt => <id+shift>-yyy.txt

Any ideas how to make it? Some awk maybe?

Best Answer

constant=42
for f in *.txt; do    # choose your pattern as appropriate.
    IFS='-.' read id suffix ext <<< "$f"
    newname="$(( 10#$id + constant ))-yyy.$ext"
    echo mv "$f" "$newname"
done

I added "10#" in the arithmetic expression to ensure the number is treated as base-10 even if it begins with a zero.

If this doesn't meet your needs, please provide more requirements in the question.