UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 6th, 2005, 11:32 AM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
Exclamation Changing file extensions

Hi,

I need help in finding how to change the file extension for all files of a particular type in a directory using c-shell script. For example, changing all .csv to .tab files in bin directory. Thank you in advance.

Reply With Quote
  #2  
Old April 6th, 2005, 02:04 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 54 m 7 sec
Reputation Power: 797
This is how to do it with sh.
Code:
#!/bin/sh
for file in *.csv
do
   newfile=`echo $file | sed "s/\.csv/.tab/"`
   mv $file $newfile
done
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old April 6th, 2005, 04:47 PM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
I did this to change the file extensions, it works but I am not sure how efficient it is and if it can be improved. I also was wondering how I could search for the directory (where the change in file extension needs to be made) that the user enters by name and how I could give the user an option of which directory to choose if there is more than one match. Thanks again for any help that you could give me.

Reply With Quote
  #4  
Old April 6th, 2005, 04:48 PM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
Sorry, here is the code:

Code:

#!/usr/bin/csh

set USAGE="USAGE: rename.csh NAME_OF_DIR .OLD_EXTENSION .NEW_EXTENSION"

if ($#argv == 3) then
foreach i ( *$1* )
  mv $i `echo $i | sed -n s/$1/$2/p`
else
echo $USAGE
exit 0

Reply With Quote
  #5  
Old April 6th, 2005, 11:19 PM
munkfish's Avatar
munkfish munkfish is offline
funky munky
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2001
Location: UK
Posts: 1,446 munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 33 m
Reputation Power: 10
Why are you using csh? It's less portable (and usable) than sh.

A nice tip for doing this kind of thing on the commandline is to create the command first using sed, and then once you're satisfied with it continue to execute the commands by adding '|sh' to the commandline. Quite similar to scorpion's example above but gets you in a little sed practise on the commandline

Makes more sense with an example - to change all php files to php.bak files:

First test your command out with this oneliner:

Code:
ls -1 | sed -e 's/\(.*)\.php/mv & \1.php.bak/


when you're happy the commands it outputs look right, just add on '|sh' to the end of it to execute it:

Code:
ls -1 | sed -e 's/\(.*)\.php/mv & \1.php.bak/ | sh


To your question about choosing a directory, maybe try incorporating find in your code, display a list of directories and then use read to read the directory entered by your users. If you need examples try this:

Reply With Quote
  #6  
Old April 7th, 2005, 10:05 PM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
I use csh because that is part of the assignment and i would appreciate it if I could get an example of how to search for the directory and how I could apply my code to this.

Reply With Quote
  #7  
Old April 7th, 2005, 11:53 PM
munkfish's Avatar
munkfish munkfish is offline
funky munky
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2001
Location: UK
Posts: 1,446 munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level)munkfish User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 33 m
Reputation Power: 10
So this is homework?

Reply With Quote
  #8  
Old April 8th, 2005, 11:59 AM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
More like preparation for a test to get into my next Unix course.

Reply With Quote
  #9  
Old April 8th, 2005, 12:32 PM
utcguy utcguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 9 utcguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 53 sec
Reputation Power: 0
I tried this but this way only changes one file, not them all:

Code:
#!/usr/bin/csh


set USAGE="USAGE: rename.csh NAME_OF_DIR .OLD_EXTENSION .NEW_EXTENSION"

set cur=$PWD
cd ~/$argv[1]
foreach i ( *$argv[2]* )
mv $i `echo $i | sed -n s/$argv[2]/$argv[3]/p`
else
echo $USAGE
endif
cd $cur
exit 0

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Changing file extensions


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway