Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can I copy file and no overwrite

Status
Not open for further replies.

sammol

MIS
Oct 10, 2001
20
HK
I have try to see the man of cp but there are not options that to set it not overwirte any file. finally I choose to use interactive mode but it is slow. Any suggestion?

Thanks
 
yoy need to set the 'noclobber' option either at the command line or in your .profile

syntax varies with each shell

ksh set -o noclobber

Check your shell man page for correct syntax
 
You might try the following.
1. Create a small script like the one below, save it as "no.sh" and set execute permission on it:

Code:
while true
do
  echo no
done

2. Use a command similar to this one: no.sh | cp -i srcfiles destdir


I cannot test this on Solaris right now, but it works on HP-UX (on HP-UX, the script is not necessary because the command "yes" can be used to do the same thing - unfortunately, I don't know if a similar command exists on Solaris).

This is somewhat dirty because you will force the answer "no" to every question. You might get unexpected results if a question different than "Overwrite (y/n)?" arises.

A better solution would be to create a script for the copy and have it verify if the destination file exists before copying.
 
Sorry, the noclobber option only works with redirection and not cp.
 
In ksh, noclobber only seems to prevent re-direction to an existing file ... cp will still overwrite.

You could write your own cp script to prevent ...

#!/bin/ksh
# cp2 - prevent overwriting

[ $# != 2 ] && {
echo &quot;Usage: cp2 <source> <destination>&quot;
exit 1
}

if [ -f $2 ]
then
echo &quot;$2 exists ... cannot copy&quot;
else
cp $1 $2
fi

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top