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.
djsal
2
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
Thanks djsal.... It works....
but I am wondernig whether we can do it in a line or not .... any idea
djsal
4
sorry man, thats the only way I know how...
Still requires multiple commands, but in the Korn shell, you can do
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
I am still looking into using the registers for doing this but anyway Thanks for your help guys .... i learned two other ways....
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
for fname in `ls *.c` do
lname=`basename $fname c`
mv $fname ${lname}cpp
done