1752591 Members
3132 Online
108788 Solutions
New Discussion юеВ

Re: Copying the files

 
CA1490051
Frequent Advisor

Copying the files

Hi ,

Can i know the command to copy all my *.cpp files to *.c files in one shot.


thanks in advance
Vikram
7 REPLIES 7
Court Campbell
Honored Contributor

Re: Copying the files

if the file are in the same directory you could do something like this:

# for file in `ls *\.cpp`
> do
> mv $file $(echo $file | awk -F\. '{print $1}').c
>done
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Court Campbell
Honored Contributor

Re: Copying the files

Sorry, I wasn't looking close enough. You need to change mv to cp if you want to copy.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
MarkSyder
Honored Contributor

Re: Copying the files

And if you want to keep the same ownership and permissions, use cp -p

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
James R. Ferguson
Acclaimed Contributor

Re: Copying the files

Hi Vikram:

The 'basename()' function (see the manpages) is your friend:

cd /path
for F in `ls -1 *.cpp`
do
cp -p `basename ${F} .cpp` `basename ${F} .cpp`.c
done

Regards!

...JRF...
spex
Honored Contributor

Re: Copying the files

Hi Vikram,

This (quasi-tested) script operates on an entire directory structure:

$ cat change_extension.sh
#!/usr/bin/sh

CMD=/usr/bin/cp
#CMD=/usr/bin/mv

if [ ${#} -lt 3 ]
then
echo "usage: ${0} " >&2
exit 1
fi

FPATH=${1}
EXTO=${2}
EXTN=${3}

find ${FPATH} -type f -name "*.${EXTO}" -print |\
while read L
do
F=${L%.${EXTO}}
${CMD} ${L} ${F}.${EXTN}
done
exit 0

For example:
$ ./change_extension.sh . cpp c

PCS
Peter Nikitka
Honored Contributor

Re: Copying the files

Hi,

another possibility:
ls *.cpp | sed 's/.cpp$//' | xargs -i cp -p '{}.cpp' '{}.c'

You can drop the single quotes, if not running in a C-shell.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Sandman!
Honored Contributor

Re: Copying the files

# ls -1 *.cpp | awk -F"cpp$" '{system("cp "$0" "$1"c")}'