Operating System - HP-UX
1821245 Members
2735 Online
109632 Solutions
New Discussion юеВ

copy directory and all files without overwriting existing files

 
SOLVED
Go to solution
Shawn Miller_2
Frequent Advisor

copy directory and all files without overwriting existing files

I have two directories directoryA and directoryB and I want to copy everything including subdirectories and files from directoryB into directoryA. However, I do not want to overwrite any files in directoryA that have the same name. Manually I could do this with cp -rpi, but there are hundreds of files and I wanted to avoid hitting n (for do not overwrite) hundreds of times. There are a lot of files that have the same name.
6 REPLIES 6
Chris Wilshaw
Honored Contributor
Solution

Re: copy directory and all files without overwriting existing files

Try this

cd /directoryB
for file_name in *
do
echo n | cp -pi $file /directoryA
done


This should then do a prompt for each file from directoryB that is encountered in directoryA, and reply to the question with n.

Rodney Hills
Honored Contributor

Re: copy directory and all files without overwriting existing files

You could try using the "pax" command-

pathA=/something/here/directoryA
pax -r -w -k -s=$pathA/== $pathA directoryB/

-k option will prevent overwriting of existing files.
-s option is a trick to munge the path names so that files under directoryA are copied relative to directoryB.

HTH

-- Rod Hills
There be dragons...
Dario_1
Trusted Contributor

Re: copy directory and all files without overwriting existing files

Hi Shawn:

Use option -i with the cp command. See -i option from man page:

Options
-i (interactive copy) Cause cp to write a prompt to standard
error and wait for a response before copying a file that would
overwrite an existing file. If the response from the standard
input is affirmative, the file is copied if permissions allow
the copy. If the -i (interactive) and -f (forced-copy)
options are both specified, the -i option is ignored.

Regards,

Dario
Darren Prior
Honored Contributor

Re: copy directory and all files without overwriting existing files

Hi,

How about:

find directoryB | cpio -pdmxlv directoryA

I'd suggest you have a read of the man page for cpio to confirm the options - especially the u option which isn't used to ensure this is what you're after.

regards,

Darren.
Calm down. It's only ones and zeros...
Ryan_123
New Member

Re: copy directory and all files without overwriting existing files

You can try 'rsync --ignore-existing'
Bill Hassell
Honored Contributor

Re: copy directory and all files without overwriting existing files

The cpio solution will accomplish what you want. On stderr, a list of existing files will be displayed but left unchanged. NOTE: the -x option should never be used when copying files. It refers to device files and these files have no meaning in any directory except /dev on the server. -x should only be used in the context of a full backup.


Bill Hassell, sysadmin