The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
change file extension from root and subdirectories Astrid Shell Programming and Scripting 10 02-17-2008 07:18 AM
extension problem please help sharmasdeepti UNIX for Dummies Questions & Answers 1 10-24-2007 07:29 AM
without extension adurga UNIX for Dummies Questions & Answers 3 07-02-2007 12:34 PM
Changing extension mohan705 Shell Programming and Scripting 4 06-30-2007 01:06 PM
how do i change extension kswaraj Shell Programming and Scripting 2 06-28-2004 08:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2002
prkwan prkwan is offline
Registered User
  
 

Join Date: Nov 2002
Location: London UK
Posts: 5
Question How to change extension?

How do you write a shell script that change the extension of all the files?

e.g

chext rtf doc

where .rtf is the original extension
and .doc is the new extension

is it something to do with basename?
do I need a for loop?
Please help!

Unix SuperNewbie
  #2 (permalink)  
Old 11-14-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
you will realize that making a search on these forums would give you more ideas... for exampe I searched for "rename files" and I got this link...

Easy way to mass rename files?

I have just modified a small piece of code from the above link...

Quote:
for name in `ls *.rtf`
do
name1=`echo $name | sed -e 's/^\(.*\)\.rtf$/\1\.doc/g'`
mv $name $name1
done
Cheers!
Vishnu.
  #3 (permalink)  
Old 11-15-2002
prkwan prkwan is offline
Registered User
  
 

Join Date: Nov 2002
Location: London UK
Posts: 5
I can get the script working as follows:

#!/bin/sh
for name in `ls *.rtf`
do
name1=` echo $name| cut -f 1 -d . `
mv $name1.rtf $name1.doc
done

is working fine, but how do I make it work like this:

chext 1 2

where 1 is the original extension and 2 is the desire new extension?
  #4 (permalink)  
Old 11-15-2002
Vishnu Vishnu is offline
Registered User
  
 

Join Date: Aug 2002
Location: Marlboro, MA
Posts: 114
replace those "rtf" and "doc" with $1 and $2 in your script...

I should add that the above way using "cut" will not work if you have multiple dots in your filename...

Code:
#!/bin/sh 
for name in `ls *.$1` 
do 
name1=`echo $name | sed -e "s/^\(.*\)\.$1$/\1\.$2/g"` 
mv $name $name1 
done
or a more compact and faster version which I prefer...

Code:
#!/bin/sh 
ls *.$1 | sed -e "s/^\(.*\)\.$1$/\1\.$1 \1\.$2/g" | xargs -n 2 mv -f
Cheers!
Vishnu.
  #5 (permalink)  
Old 11-16-2002
prkwan prkwan is offline
Registered User
  
 

Join Date: Nov 2002
Location: London UK
Posts: 5
thanks vishnu!!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0