views:

128

answers:

5

I have a bunch of files on my server that look like below. I need to write a script that will rename all the files from JPG to .jpg as you can see some of them already have the .jpg extension and some don't.

Can you help me? I could do it in either bash or through php, whatever is easier, I just don't know how.

Thanks

Jason

jects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4742JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4749JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4806JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4726JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4716JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/AGSM-Ground-Floor-2010-036JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4728JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4736JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4724JPG.jpg
media/projects/Education/St_Gregorys_School/IMG0100JPG
media/projects/Education/St_Gregorys_School/IMG0099JPG
media/projects/Education/St_Gregorys_School/IMG0092JPG
media/projects/Education/St_Gregorys_School/IMG0090JPG
media/projects/Education/St_Gregorys_School/IMG0084JPG
media/projects/Education/St_Gregorys_School/IMG0093JPG
media/projects/Education/St_Gregorys_School/IMG0097JPG
media/projects/Education/St_Gregorys_School/IMG0098JPG
media/projects/Education/St_Gregorys_School/IMG0085JPG
media/projects/Education/St_Gregorys_School/IMG0091JPG
media/projects/Education/St_Gregorys_School/IMG0094JPG
media/projects/Education/St_Gregorys_School/IMG0088JPG
media/projects/Education/St_Gregorys_School/IMG0087JPG
media/projects/Education/St_Gregorys_School/IMG0095JPG
media/projects/Education/St_Gregorys_School/IMG0096JPG
media/projects/Education/St_Gregorys_School/IMG0089JPG
media/projects/Education/St_Gregorys_School/IMG0086JPG
+4  A: 
find . -name '*JPG.jpg' -print0 | xargs -0 rename JPG.jpg .jpg
find . -name '*JPG' -print0 | xargs -0 rename JPG .jpg

rename(1) is a standard tool from util-linux

wRAR
That will not rename the files that do not contain the dot before the JPG. So e.g 'media/projects/Education/St_Gregorys_School/IMG0086JPG' will not be renamed
Kai Inkinen
OK, fixed that.
wRAR
I used this fix and it works well, thanks, however - would it avoid renaming files that already have an extension/
Jason
A: 

This is only loosely untested, but should work....

printf '#!/bin/sh\nmv -v $1 "$1".jpg' > addext.sh
chmod u+x addext.sh
find . -type f -name "*[0-9]JPG"  -exec ${PWD}/addext.sh "{}" ";"

That assumes you're running with GNU tools (which you will be, since you've tagged the question as linux).

Tim
A: 

I would go for this beast. Not the most elegant version, but will rename any files ending with (case-insensitive) JPG.jpg

find . -type f | grep -i -E '(JPG)*\.*(JPG)+$' | while read file ; do mv $file `echo $file | perl -pe 's!(JPG)*\.*(JPG)+$!.jpg!gi'`; done
Kai Inkinen
A: 

Given the filename patterns in your example, this will do the trick:

for F in *[0-9]JPG; do mv $F $F.jpg; done

If you need a recursive solution:

find . -name "*[0-9]JPG" -exec mv \{\} \{\}.jpg \;
Simon Whitaker
This is not recursive.
wRAR
I know, hence "given the filename patterns in your example" - he doesn't need a recursive solution in this instance.
Simon Whitaker
Adding recursive alternative...
Simon Whitaker
I used this fix and it worked well except I had to modify it slightly...find . -iname "*[a-z|A-Z|0-9]JPG.jpg"
Jason
A: 

bash 4

#!/bin/bash
shopt -s globstar
for file in **/*JPG
do
  mv "$file" "${file/JPG/.jpg}"
done
ghostdog74
zsh knows `**/*` globbing too.
wRAR