Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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.