extension changing

hi!i need to write a shell script which changes the extensions of files in a directory.
For example, if the name of the script is ext.sh then a call such as below
ext.sh c cpp
changes the extensions of files which are "c" to "cpp"..

Need your help, thanks..
btocakciAsked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
Hi btocakci,

find . -type f -name "*.$1" | sed "s:\(.*\)\.$1:\1:" | while read filename
do
      mv $filename.$1 $filename.$2
done

Cheers!
Sunnycoder
0
 
Heem14Commented:
FWIW, you can do this without a script as well


rename .c .cpp *.c

or for another example

rename .txt .text *.txt

0
 
btocakciAuthor Commented:
hi sunnycoder!
which variable is c and which one is cpp??
how can adapt your command as i want? (c to cpp)
0
 
sunnycoderCommented:
>ext.sh c cpp
>changes the extensions of files which are "c" to "cpp"..
$1 is the first argument ... c in your example ... $2 is the second argument ... cpp in your example
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.