0

Preface: I am an absolute novice with Linux, please don't judge strictly.

I have a directory containing several images (over 100) of a .pnm extension. I wish to rename them in such a sense, that their names should be "img1.pnm", "img2.pnm", "img3.pnm", etc. So they should have a common part in their names––"img" and different indicies running from "1".

How can I do this?

Archemar
  • 31,554

2 Answers2

2

Try the following...

unset i; for f in *.pnm; do mv "$f" "img$((++i)).pnm";done
0

try

 ls *.pnm | awk -F. '{printf "mv %s img%d.pnm\n",$0,NR;}' | bash

where

  • ls *.pnm | make a list of pnm's file
  • awk -F. '{printf "mv %s img%d.pnm\n",$0,NR;}' launch awk
    • -F. use . as separator
    • '{printf "mv %s img%d.pnm\n",$0,NR;}' print instruction to move and number file.
  • | bash instruction are give to bash.

you can even preview with

ls *.pnm | awk -F. '{printf "mv %s img%d.pnm\n",$0,NR;}'
Archemar
  • 31,554
  • I've tried this way and the names were successfully changed, however I cannot open a single file after it. Something happened to the files themselves – Dmitry Kazakov Oct 30 '14 at 15:48