4

I have 2 folders , A and B. they are similar, but there are some files in A that are not present in B. what's the best way to copy only the new files in A into B, without changing the existing files in B?

GSto
  • 391

2 Answers2

4

You can use the --update option to rsync:

cd A
rsync -a --update . ../B/

And you can do something similar using tar's --skip-old-files option:

cd A
tar -cf- . | tar -C ../B -xv --skip-old-files -f-
larsks
  • 44,886
  • 15
  • 124
  • 183
  • With rsync you'd want --ignore-existing if you don't want to replace the files that have the same names but different contents. – DerfK Nov 20 '10 at 04:26
  • This is a pretty old question and answer, but still: I think the cpio example above is wrong. In my tests, cpio would always overwrite an older existing file in B with a newer version in A. This is not what the OP was asking for. And I don't see a way around it in the cpio manual. – Carsten Feb 23 '18 at 23:08
  • @Carsten, you're right! I replaced it with a tar example. – larsks Feb 24 '18 at 01:04
1

cp with -n.