Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted
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"

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.

share|improve this answer
it's quite good, only problem is that 'xxx' part is variable, so i need to extract id and that part separately, modify id and merge again –  ts01 Feb 5 '13 at 5:04
Does 'xxx' contain dashes or dots? If not, then this code will work: the suffix variable will contain whatever 'xxx' is. If there can be dashes and dots, I can provide alternatives, but this is exactly the kind of information you need to provide in the question. –  glenn jackman Feb 5 '13 at 22:59
ok, I figured this out (IFS was quite new for me) and I made it working. Thanks a lot –  ts01 Feb 6 '13 at 3:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.