Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename and Increment Inconsistent Filenames 1

Status
Not open for further replies.

mrimagepueblo

Programmer
Dec 15, 2003
52
US
I have a need to rename .jpg files coming in with inconsistent ending to something I can run a better if statement on in perl script I use. The front part of the filename is always consistent but the back end of the file can be anything.
I am wanting to write a shell script in #!/bin/sh to rename the files. There can anywhere from 1 to 8 .jpg files per occurence of a particular number.

1059133_124025.jpg
1059133_127329.jpg
1059133_145098.jpg
1059133_179041.jpg
1059133_134855.jpg
1059133_236549.jpg
1059133_234539.jpg
1059133_456987.jpg

1059145_170946.jpg
1059145_168902.jpg
1059145_234987.jpg
1059145_123494.jpg
1059145_232990.jpg
1059145_234380.jpg
1059145_129042.jpg
1059145_289333.jpg

I would like the files renamed keeping the front part of the filename to the _ and then incementing the back part to either _1.jpg, _2.jpg, _3.jpg,_4.jpg,_5.jpg,_6.jpg,_7.jpg,_8.jpg
or,
_a.jpg,_b.jpg,_c.jpg,_d.jpg,_e.jpg,_f.jpg,_g.jpg,_h.jpg
so the filenames would look like.

1059133_a.jpg
1059133_b.jpg
1059133_c.jpg
1059133_d.jpg
1059133_e.jpg
1059133_f.jpg
1059133_g.jpg
1059133_h.jpg
or
1059145_1.jpg
1059145_2.jpg
1059145_3.jpg
1059145_4.jpg
1059145_5.jpg
1059145_6.jpg
1059145_7.jpg
1059145_8.jpg

any help would greatly be appreciated.
 
Part #1
Code:
a=0; for dat in 1059133_*.jpg ; do mv $dat 1059133_$a.jpg; a=$(($a+1)); done
#
# or 
#
a=0; 
for dat in 1059133_*.jpg ; 
do 
    mv $dat 1059133_$a.jpg
    a=$(($a+1))
done
Part #2 analog

seeking a job as java-programmer in Berlin:
 
This works fine if I know the first part of the number 1059133. What would it be if I wanted to use a wildcard for the first set of numbers? In other words, I would be downloading everyday a set of pictures, and don't necesarily want to have to hard code in the 1059133 to make the conversion. Obviously following your example *_* renames all the files to *_1.jpg, etc.
I'm not sure I understand your Part#2 analog statement either?
 
The use two loops (untested code):

Code:
#!/bin/sh
DEBUG=""
DEBUG=echo # omit or comment out this line to run for real
for first in `ls *_*.jpg|cut -d_ -f1|sort -u`
do
 a=1
 for dat in $first_*.jpg
 do
  $DEBUG mv $dat $first_$a.jpg
  a=`expr $a + 1`
 done
done


HTH,

p5wizard
 
p5wizard it looks like this one is clobbering everything in front of the .jpg in Echo mode and just renaming 1.jpg, 2.jpg, etc. When I run it, it wipes out all the files and leaves me with a 1.jpg and I get consecutive errors for the number of files that are in the directory.
mv: `1.jpg' and `1.jpg' are the same file
 
OK, so put braces around the variable names:

Code:
#!/bin/sh
DEBUG=""
DEBUG=echo # omit or comment out this line to run for real
for first in `ls *_*.jpg|cut -d_ -f1|sort -u`
do
 a=1
 for dat in ${first}_*.jpg
 do
  ${DEBUG} mv ${dat} ${first}_${a}.jpg
  a=`expr $a + 1`
 done
done

I told you it was untested... You didn't run it with DEBUG first? I hope you ran it on test dir first.


HTH,

p5wizard
 
p5wizard, thanks a million, exactly what I was looking for.
Not to worry, I caught your comment about untested. I only pulled one file off the server, just had to download the file again.
Thanks again. Someday, I'll be able to follow all this logic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top