Tal_
Tal_

Reputation: 761

Quick way to modify to multiple filenames in Linux

I have files:

1.pgm ... 400.pgm consecutive numbers from 1 to 400 all in same format.

I want to copy them into:

401.pgm ... 800.pgm, it's basicaly because I need more input data

I'm wondering if there's any quick way to do this in Linux?

Upvotes: 2

Views: 69

Answers (2)

Olivier Dulac
Olivier Dulac

Reputation: 3791

If your file have this structure, then simply:

for file in *.pgm ; do
   num=${file%%.pgm}
   newnum=$(( $num + 400 ))  #more portable than 'let', thx to @user2719058 for the reminder!
   echo mv "${file}" "${newnum}.pgm"
done

and take out the echo once you're confident that this does what you want...

Upvotes: 4

user2719058
user2719058

Reputation: 2233

With rename(1):

rename 's/(\d+)/400+$1/e' *.pgm

That's a separate program, though. A slightly less elegant way in pure bash is

for f in *.pgm; do mv $f $((400+${f//[!0-9]/})).pgm; done

Upvotes: 1

Related Questions