6

How would it be possible to copy all new contents from one directory to another so that only new files are copied from the source directory (both directories have the same naming tree). For example, here is the layout of directory A:

/dirA
     a.php
     b.txt
     subdirA1/
              readme.txt
              config
              source_file1.c

/dirB
     c.php
     subdirA1/
              readme.txt

at the end dirB should have all the new files in dirA. Assume that there are only new files in dirA and its sub directories. The result should be the union of the two directories:

/dirB
     a.php
     b.txt
     c.php
     subdirA1/
              readme.txt
              config                  
              source_file1.c

I have tried using cp -ra:

cp -ra dirA/* dirB/

but dirB is completely overwritten by dirA.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Sebi
  • 1,009

1 Answers1

8

rsync was designed exactly to solve this problem:

[$]> rsync -av --ignore-existing dirA/ dirB/