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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to change file extension? 1

Status
Not open for further replies.

nrastogi

Programmer
Jul 19, 2002
52
US
Hi guys,

I am very new to the scripting world.

I have this directory that has 1000's files with the extension .JPG and .jpg as well.

Since, *ix is case sensitive, that is creating a problem for me.

Could you please let me know the script that would go through all the files and change the extension from JPG to jpg.

I appreciate your help!

Thanks a lot in advance.

NRastogi
 
Hi guys,

Thought to mnention that I am using RedHat 7.3 Linux !

Thanks,
NRastogi
 
Something like this should work
Code:
for file in `find . -type f -name "*.JPG"`        
do
  base=`basename $file .JPG`
  mv $file $base.jpg
done
Not that, by default, find recurses through directories. Hope this helps, cheers Neil :)
 
Hi there,

Thanks a lot for posting the script.

I am totally newbie to scripts, could you please let me know the exact steps to run this script.

What I understand I have to save this in a .sh, say chang.sh

how do I run it and pass the parameter from the promt

Thanks

nrastogi
 
The code above can either be typed directly at your command line prompt, or you can copy the code to a file such as [tt]change.sh[/tt]. All you would need to do for the script version is ensure that the first line in the script resembles:

[tt]#!<path to bash>[/tt]

Linux uses bash (Bourne again shell). To find out where your version of bash is, try typing the following at the command line prompt:

[tt]which bash[/tt]

Then if for example this returns [tt]/usr/bin/bash[/tt], then have the following as the first line in your script:

[tt]#!/usr/bin/bash[/tt]

The example code I showed does not use any parameters. Hope this helps. Cheers, Neil

 
An example using parameters:
Code:
#!<path to bash>

UPPERCASE=$(echo $1 | tr &quot;[:lower:]&quot; &quot;[:upper:]&quot;)
LOWERCASE=$(echo $1 | tr &quot;[:upper:]&quot; &quot;[:lower:]&quot;)

for FILE in $(find . -type f -name &quot;*.$UPPERCASE&quot;)
do
  BASE=$(basename $FILE .$UPPERCASE)
  DIR=$(dirname $FILE)
  echo mv $FILE $DIR/$BASE.$LOWERCASE
  mv $FILE $DIR/$BASE.$LOWERCASE
done

Cheers, Neil :)
 
Thanks a lot Neil,

You have give a life to this newbie :)

Well, you script did work, I just had to put -f in the mv command line, it was asking me yes or no to overwrite. Which was kinda hard to do manually for 7000 files :)

Thanks a lot again.

NRastogi
 
One minor refinement when you have a large number of files. Change:

for FILE in $(find ...)

To:

find .... | while read FILE

I believe this is more efficient.

Cheers, Neil
 
Or another way...........
for file in *JPG
do
mv $file `echo $file | sed -e 's/JPG/jpg/g'`
done
;-)
Dickie Bird
Honi soit qui mal y pense
 
Hi Guys,

Thanks you all for responding to my question.

I have another issue that is related to same question:

I would like to change the full filename (name + extension) to lowercase of all the files in a directory, including sub-dirs of that file

Please let me know how could I modify the script you have mentioned earlier to get this done.

Thanks,

NRastogi
 
dbird: you my be have some 'c' troubles, i congratulate
you for this 'jamisar'conform one-liner. bravo!
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
i forgot:

for file in *.JPG
do
.... sed -e 's/JPG$/jpg/' ...
done

is a little saver, to prevent:

mv aaa.JPG.JPG aaa.jpg.jpg

i like people doing reasoned things :)) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Hi Neil,

>>>> The find version already does this
Cheers, Neil

I did change the script to use find. but how do I pass the parameter from command line to use this new script that would convert UPPERCASE.EXTENSION to lowercase.extenstion

Thanks much,

NRastogi

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top