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.

3
  • 2
    Did you checkout rsync?
    – Vikyboss
    Mar 21, 2016 at 16:34
  • Yep, it worked rsync -a dirA/ dirB :P
    – Sebi
    Mar 21, 2016 at 16:42
  • 2
    @Sebi rsync -a dirA/ dirB/ overwrites the files in dirB with files by the same name from dirA, just like cp -a. What you're looking for is rsync -a --ignore-existing. Mar 21, 2016 at 23:00

1 Answer 1

8

rsync was designed exactly to solve this problem:

[$]> rsync -av --ignore-existing dirA/ dirB/
2
  • 2
    Er, yes, it is, but you have to use the right option! Mar 21, 2016 at 22:59
  • Oops! That's what I get for being quick about things and not testing them out. Mar 23, 2016 at 20:11

Not the answer you're looking for? Browse other questions tagged .