|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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:
__________________
FreeBSD Admin Tips Tricks and Scripts |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
||||
|
||||
|
So this is homework?
|
|
#8
|
|||
|
|||
|
More like preparation for a test to get into my next Unix course.
|
|
#9
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Changing file extensions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|