LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script question (http://www.linuxquestions.org/questions/linux-general-1/shell-script-question-788944/)

poorboyiii 02-13-2010 05:22 PM

Shell script question
 
If someone could help with a simple shell program it would be greatly appreciated.

I have a directory with several files in it each having an extension of ".JPG" and need to change the extension to lower case ".jpg".
I wrote the following:
--------------------------
#!/bin/bash

for ofn in *.JPG
do
cnt=${#ofn}-3
nfn=${ofn:0:cnt}"jpg"
echo $ofn $nfn #not necessary but shows the value of the two names
mv -v $ofn $nfn
done
--------------------------

The directory contains the shell program and the following:
0614 Kite.JPG
0615 Kite.JPG
0616 Kite.JPG
0617 Kite.JPG
0618 Kite.JPG
0619 Kite.JPG
0620 bushes.JPG
0621 plants.JPG

The output is:
0614 Kite.JPG 0614 Kite.jpg
mv: target `Kite.jpg' is not a directory
0615 Kite.JPG 0615 Kite.jpg
mv: target `Kite.jpg' is not a directory
0616 Kite.JPG 0616 Kite.jpg
mv: target `Kite.jpg' is not a directory
0617 Kite.JPG 0617 Kite.jpg
mv: target `Kite.jpg' is not a directory
0618 Kite.JPG 0618 Kite.jpg
mv: target `Kite.jpg' is not a directory
0619 Kite.JPG 0619 Kite.jpg
mv: target `Kite.jpg' is not a directory
0620 bushes.JPG 0620 bushes.jpg
mv: target `bushes.jpg' is not a directory
0621 plants.JPG 0621 plants.jpg
mv: target `plants.jpg' is not a directory


Thanks Sam

jhcaiced 02-13-2010 05:31 PM

Hi,

The problem seems to be the spaces in your file names, add quotes to the mv command to avoid this.

mv -v "$ofn" "$nfn"

Best regards,

poorboyiii 02-13-2010 05:41 PM

Quote:

Originally Posted by jhcaiced (Post 3862878)
Hi,

The problem seems to be the spaces in your file names, add quotes to the mv command to avoid this.

mv -v "$ofn" "$nfn"

Best regards,

That worked Thanks.....

Sam

GazL 02-13-2010 05:42 PM

Take a look at the rename command.
Code:

rename .JPG .jpg *.JPG


All times are GMT -5. The time now is 09:07 PM.