Re: Copying the files (157 Views)
Reply
Frequent Advisor
CA1490051
Posts: 81
Registered: ‎06-07-2007
Message 1 of 8 (157 Views)

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
Honored Contributor
Court Campbell
Posts: 1,605
Registered: ‎08-12-2004
Message 2 of 8 (157 Views)

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???"
Honored Contributor
Court Campbell
Posts: 1,605
Registered: ‎08-12-2004
Message 3 of 8 (157 Views)

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???"
Honored Contributor
MarkSyder
Posts: 1,815
Registered: ‎11-10-2003
Message 4 of 8 (157 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 8 (157 Views)

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...
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 6 of 8 (157 Views)

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
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 7 of 8 (157 Views)

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"
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 8 of 8 (157 Views)

Re: Copying the files

# ls -1 *.cpp | awk -F"cpp$" '{system("cp "$0" "$1"c")}'
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.