The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get files with a specific extension janavenki Shell Programming and Scripting 9 11-27-2007 05:56 PM
Feedback: Allow tar.gz extension for files DukeNuke2 Post Here to Contact Site Administrators and Moderators 2 08-29-2007 10:22 AM
Removing the extension and FTP the files pradkumar Shell Programming and Scripting 1 02-14-2007 11:01 AM
How to test for files with a particular extension. jyotipg UNIX for Dummies Questions & Answers 1 05-09-2002 04:04 AM
How to list files will no extension Wing m. Cheng UNIX for Dummies Questions & Answers 5 02-21-2002 11:34 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-27-2004
Registered User
 

Join Date: Apr 2004
Posts: 17
replace *.sqc files with *.sqC extension

Hi everyone,

I want to know how to replace file extensions. For example how do i go about replacing all *.c files in a directory to *.cpp

I think we can do it either with "find" commad or with special registers. I tried to use the following command

mv \(.*\).c \1.cpp

but it is giving an error saying, cant stat source (.*).c
whats wrong with this ?


I am not sure how to do it with find commnad. please let me know if anyone of you know how to do it.
__________________
Ramesh

Last edited by rameshonline; 04-27-2004 at 01:44 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 04-27-2004
Registered User
 

Join Date: Apr 2004
Location: Long Island Ny
Posts: 23
This will do the trick!

#Korn shell or Bash

for list in `ls -1t *.c*`
do
prefix=`echo $list | awk -F"\." '{print $1}'`
mv $list ${prefix}.cpp
done
Reply With Quote
  #3  
Old 04-27-2004
Registered User
 

Join Date: Apr 2004
Posts: 17
Thanks djsal.... It works....

but I am wondernig whether we can do it in a line or not .... any idea
__________________
Ramesh
Reply With Quote
  #4  
Old 04-27-2004
Registered User
 

Join Date: Apr 2004
Location: Long Island Ny
Posts: 23
Quote:
Originally posted by rameshonline
Thanks djsal.... It works....

but I am wondernig whether we can do it in a line or not .... any idea
sorry man, thats the only way I know how...
Reply With Quote
  #5  
Old 04-27-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Still requires multiple commands, but in the Korn shell, you can do
Code:
for file in *.c; do _file=${file%%.c}.cpp; mv $file $_file; done
on a single line, although it hampers readability....

That will rename all .c files to .cpp in the current directory.

Peace,
ZB
http://www.zazzybob.com
Reply With Quote
  #6  
Old 04-27-2004
Registered User
 

Join Date: Apr 2004
Posts: 17
I am still looking into using the registers for doing this but anyway Thanks for your help guys .... i learned two other ways....
__________________
Ramesh
Reply With Quote
  #7  
Old 04-28-2004
Registered User
 

Join Date: Apr 2004
Posts: 17
one other interesting way to do it

Hi Guys ...I found one other way of doing it. Try this


ls *.c | awk -F '.' '{print "mv "$1".c "$1".cpp"}'| csh


It does the trick. Thanks
__________________
Ramesh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0