changing filenames in a directory to a number within a loop

most welcome - y'all come back now, y'hear!

on the SECOND run, you're mv-ing '1.jpg' to '1.jpg', right?
The system have problems over-writting the same file....
try 'mv -f' - start from the beginning and do 2 runs as you have already.

ya on the second run it's changing the same filename to the same filename. the reason why this is important, is because i will be adding images to the same directory later, and i'd like to just rename the newest photos to be in line with the rest of them. but i don't want to lose any data either. i tried the '-f' flag but it still does the same thing. but after i do it a 3rd and 4th time, it doesn't change anything. it just keeps 10.jpg - 19.jpg. does that help?

well..... here's the same script with the 'echo'.
first run:

mv Luis Work 001.jpg 1.jpg
mv Luis Work 008.jpg 2.jpg
mv Luis Work 009.jpg 3.jpg
mv Luis Work 010.jpg 4.jpg
mv Luis Work 016.jpg 5.jpg
mv Luis Work 017.jpg 6.jpg
mv Luis Work 018.jpg 7.jpg
mv Luis Work 028.jpg 8.jpg
mv Luis Work 029.jpg 9.jpg
mv Luis Work 030.jpg 10.jpg
mv Luis Work 042.jpg 11.jpg
mv Luis Work 043.jpg 12.jpg
mv Luis Work 044.jpg 13.jpg
mv Luis Work 045.jpg 14.jpg
mv Luis Work 046.jpg 15.jpg
mv Luis Work 055.jpg 16.jpg
mv Luis Work 068.jpg 17.jpg
mv Luis Work 069.jpg 18.jpg
mv Luis Work 070.jpg 19.jpg

here's the second run:

mv 1.jpg 1.jpg
mv 10.jpg 2.jpg
mv 11.jpg 3.jpg
mv 12.jpg 4.jpg
mv 13.jpg 5.jpg
mv 14.jpg 6.jpg
mv 15.jpg 7.jpg
mv 16.jpg 8.jpg
mv 17.jpg 9.jpg
mv 18.jpg 10.jpg
mv 19.jpg 11.jpg
mv 2.jpg 12.jpg
mv 3.jpg 13.jpg
mv 4.jpg 14.jpg
mv 5.jpg 15.jpg
mv 6.jpg 16.jpg
mv 7.jpg 17.jpg
mv 8.jpg 18.jpg
mv 9.jpg 19.jpg

as you can see some of the files get overwritten..

you can modify sort - to sort numerically this will keep the files - not sure it'll fix all that you want, but.....

sort -n
ls -1 *.jpg | awk -F"." '{system("mv \""$0"\" "++i"."$NF)}'

reading through this thread its rather obvious why you're encountering the errors.

one of your examples:
kumi@throne:atlas/images/photos % ls
10.jpg 12.jpg 14.jpg 16.jpg 18.jpg 1.jpg 21.jpg 23.jpg 25.jpg 27.jpg 29.jpg 30.jpg 32.jpg 34.jpg 36.jpg 4.jpg 6.jpg 8.jpg gallery*
11.jpg 13.jpg 15.jpg 17.jpg 19.jpg 20.jpg 22.jpg 24.jpg 26.jpg 28.jpg 2.jpg 31.jpg 33.jpg 35.jpg 3.jpg 5.jpg 7.jpg 9.jpg
kumi@throne:atlas/images/photos % ./gallery
mv: overwrite `1.jpg', overriding mode 0644? y

you're original requirement was the take <sometext>.jpg and move it to <somenumber>.jpg using sort, all good and fine.
However it seems from what I see, and what you wrote that you went back and reran the script on this directory again. Earlier someone did note that sort is an ascii sort, not numeric, so the first file the script encounters is 10.jpg ( not 1.jpg) , and this file is already in the format you stated you want.. so why would you want to move it??

then doing a mv -f will move it and sure enough you list files... working as I'd expect.

Unless you're going to run this once on a directory that is not already been converted to <number>.jpg, "and has zero <number>.jpg already existing" you'll need to setup some more logic to:

  1. decide if the file is already in the correct format, if so do you want to skip it or not... again note that your ls is a ascii sort, so you're file listing is not really in the order you'd 1st expect.
  2. does the filename i.e. <somenumber index>.jpg you're wanted to move the filename to already exist? is so increment index and retest if exists.

@vgersh, of course! that's why i'm losing files, because it's overwriting existing ones before the loop gets to them. and that's where the sort may be able to help. thanks for clearing that up!

@shamrock, your code works perfectly the first time, but like the last script i was working with: if executed a second time, i lose files.

@denn, 'this file is already in the format you stated you want.. so why would you want to move it??', well i'll be adding more photos days later to the gallery, and i'd like to just run the command again, and have the filenames follow the ascending order without disturbing the original (already altered) filenames. and i agree, we're going to need to throw some logic in there, but i would still like to ignore the check to see if the file is already in the correct format, because sometimes, i'll be adding files with intigers in them, but they may be the wrong number. for instance, let's say i have 1.jpg - 10.jpg in a directory. then i add some images like 57.jpg and 1152.jpg. since the filename is an intiger, it's fine, right? no. all of them have to be in ascending order, one behind the other. there can't be any gaps in the integers. so to solve this problem i think all we need is a more intelligent sort and it should work no problem. i'll see what i can do with this.

thank you all for your help and guidance!

alright check it out guys. here's the script:

#!/bin/bash

ls -a1 *.$1 | sort -n | while IFS= read fname ; do
    #echo "mv $fname ${i}.${1}"
    mv -f "$fname" "${i}.${1}"
    ((i++))
done

and here's the outcome:

kumi@throne:atlas/images/photos % ls
gallery*           Luis Work 009.jpg  Luis Work 017.jpg  Luis Work 029.jpg  Luis Work 043.jpg  Luis Work 046.jpg  Luis Work 069.jpg
Luis Work 001.jpg  Luis Work 010.jpg  Luis Work 018.jpg  Luis Work 030.jpg  Luis Work 044.jpg  Luis Work 055.jpg  Luis Work 070.jpg
Luis Work 008.jpg  Luis Work 016.jpg  Luis Work 028.jpg  Luis Work 042.jpg  Luis Work 045.jpg  Luis Work 068.jpg  untitled folder/
kumi@throne:atlas/images/photos % ./gallery 
ls: *.: No such file or directory
kumi@throne:atlas/images/photos % ./gallery jpg
kumi@throne:atlas/images/photos % ls
10.jpg  12.jpg  14.jpg  16.jpg  18.jpg  2.jpg  4.jpg  6.jpg  8.jpg  gallery*
11.jpg  13.jpg  15.jpg  17.jpg  1.jpg   3.jpg  5.jpg  7.jpg  9.jpg  untitled folder/
kumi@throne:atlas/images/photos % ./gallery jpg
kumi@throne:atlas/images/photos % ls
10.jpg  11.jpg  12.jpg  13.jpg  14.jpg  15.jpg  16.jpg  17.jpg  1.jpg  2.jpg  3.jpg  4.jpg  5.jpg  6.jpg  7.jpg  8.jpg  9.jpg  gallery*  untitled folder/

i think we're close! i lost one image and it was on the first run. i didn't lose anything the second time, but the files were listed in a different fashion, which i thought was weird. anyway what do you guys see?

What do you mean by losing files? I am unable to follow that please explain.

ls -1 *.jpg | \
awk -F"." '
{
  if ($0~"^[0-9]+.jpg$")
     i++
  else
     system("mv \""$0"\" "++i"."$NF)
}'

shamrock your script works almost perfectly. check out these circumstances:

kumi@throne:atlas/images/photos % ls
313.jpg                        gallery*           Luis Work 017.jpg  Luis Work 044.jpg  pic 2.jpg          Wilsonart.jpg
7.jpg                          Karndean_000.jpg   Luis Work 018.jpg  Luis Work 045.jpg  pic 3.jpg          Wilsonart_Picture1.jpg
BA2.jpg                        Luis Work 001.jpg  Luis Work 028.jpg  Luis Work 046.jpg  pic 4.jpg
Bretlin_Closeup_Picture.jpg    Luis Work 008.jpg  Luis Work 029.jpg  Luis Work 055.jpg  pic 5.jpg
Cherry floor.jpg               Luis Work 009.jpg  Luis Work 030.jpg  Luis Work 068.jpg  productcarpet.jpg
Daltile.jpg                    Luis Work 010.jpg  Luis Work 042.jpg  Luis Work 069.jpg  Tile.jpg
EW01-A4DiningLounge-FEB06.jpg  Luis Work 016.jpg  Luis Work 043.jpg  Luis Work 070.jpg  untitled folder/
kumi@throne:atlas/images/photos % ./gallery 
kumi@throne:atlas/images/photos % ls
10.jpg  12.jpg  14.jpg  16.jpg  18.jpg  20.jpg  22.jpg  24.jpg  26.jpg  28.jpg  30.jpg   31.jpg  33.jpg  35.jpg  4.jpg  6.jpg  8.jpg  gallery*
11.jpg  13.jpg  15.jpg  17.jpg  19.jpg  21.jpg  23.jpg  25.jpg  27.jpg  29.jpg  313.jpg  32.jpg  34.jpg  3.jpg   5.jpg  7.jpg  9.jpg  untitled folder/

check out the image 313.jpg. it remains unchanged after the script is executed. i am not familiar with awk (altho i wish i was because it is powerful as hell), but i'm sure there's a way to fix it. must be huh? what do you think?