0

Given a directory of files:

00012.png, 00013.png, 00014.png etc...

What is the simplest way to rename the entire batch to:

00001.png, 00002.png, 00003.png etc...

I've looked up the rename utility but am feeling baffled.

There a number of other questions similar in nature to this but they are very specific (e.g: "how do I remove this underscore and three random letters"), and so they're normally answered with a similar degree of specificity. I just can't find a solution to this precise problem.

s-low
  • 706
  • 2
  • 8
  • 21
  • Here's a 1 liner. `for file in *.png; do printf "mv '%s' '%05d.png'\n" "$file" $(( i++ )); done`. For your protection this will only print `mv 'oldname' 'number.png'`. – alvits Aug 06 '15 at 23:05

2 Answers2

1

If you just want to number the files, use a simple counter:

# set counter to zero
i=0
for file in *png; do 
    # move file
    echo mv "$file" "$(printf "%05d.png" ${i})"
    # increase counter
    ((i++))
done

With given filenames 00012.png 00013.png 00014.png this results in

mv 00012.png 00000.png
mv 00013.png 00001.png
mv 00014.png 00002.png

Please remove the echo, i just added it for testing.

chepner
  • 497,756
  • 71
  • 530
  • 681
redimp
  • 1,061
  • 8
  • 12
-1

Would try something like this:

ls *.png | while read file; do
    mv "$file" "$(printf %05d $(expr $(echo $file | cut -d. -f1) - 11)).png"
done;

(you may replace "mv" by "echo" for testing purpose)

$ touch 00012.png 00013.png 00014.png
$ ls *.png | while read file; do echo "$file" "$(printf %05d $(expr $(echo $file | cut -d. -f1) - 11)).png";done;
00012.png 00001.png
00013.png 00002.png
00014.png 00003.png
Blusky
  • 3,470
  • 1
  • 19
  • 35