2

I want to rename a lot of files (some jpg and a lot of png files) I need them sequentially numbered on each folder have this structure:

.../folder01
    file.png
    file.jpg
.../folder02
    file.png
    file.png
  ..........
.../folder05
    file.png
    file.png

and I want something like this:

.../folder01
    0001.jpg
    0002.png
.../folder02
    0003.png
    0004.png
  ..........
.../folder05
    0012.png
    0013.png      

how can I make it using bash?

mao2047
  • 91
  • 1
  • 8

2 Answers2

3

Here's one way:

find . \( -name '*.jpg' -o -name '*.png' \) -print  | (i=0; while read f; do 
    let i+=1; mv "$f" "${f%/*}/$(printf %04d "$i").${f##*.}"; 
done)
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • thanks a lot! this works magnific! just one thing. in the case I test (folders 01 02 03) the minor numbers are in the last folder: 01 --> 0006.png 0005.png and 03 --> 0001.png 0002.png , I want the opposite. – mao2047 Jan 09 '13 at 04:07
  • 1
    add a `sort`: `find` ... `-print | sort | (i=0;` ... and the files will be sorted before they get numbered, so folder01 will come before folder02, etc. – Mark Reed Jan 09 '13 at 14:50
0

Here is another way using the automated tools of StringSolver:

mv folder01/file.jpg folder01/0001.jpg
mv folder01/file.png folder01/0002.png
mv

The second example is needed because else it would think about renaming all files using the number in the folder's name. The last two lines can also be abbreviated in just one, which performs the moves and immediately generalizes it:

mv -a folder01/file.png folder01/0002.png

DISCLAIMER: I am a co-author of this work for academic purposes, and working on a bash script renderer. But you can already test the system as it.

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101