Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the command to copy a folder, and its contents, to a location with the following rules:

Possibility 1: if the folder exists already, preform a force merge WITHOUT overwriting existing files.

AND do not overwrite existing files

Possibility 2: If the folder does not exist, create it and copy the contents to the newly created folder

Thanks, Christopher Peterson

share|improve this question

migrated from stackoverflow.com Jan 28 '12 at 0:27

This question came from our site for professional and enthusiast programmers.

    
It is important to understand that possibility-1 has two forms: the existing files may be older or newer compared to your source folder contents. It this matters, you will need to understand the answers below better... – nik Jan 28 '12 at 7:05
up vote 4 down vote accepted

cp <base-target> <base-destination> -r --update

-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing

-R, -r, --recursive copy directories recursively

share|improve this answer
    
Note: The '-u' option will over write an existing file that is older than the one being copied. This does not quite follow part of the requirements... check the --no-clobber option if that is not what you want. – nik Jan 28 '12 at 7:08

You can use the --no-clobber option.

cp --recursive --no-clobber SOURCE DESTINATION

From the man page:

-R, -r, --recursive
       copy directories recursively
-n, --no-clobber
       do not overwrite an existing file (overrides a previous -i option)
share|improve this answer
    
The first answer by @Michael will not do what you want. If the file in the source directory is newer that the destination file it will overwrite it, so requirement 1 will not be met. – Fotis Jan 28 '12 at 0:49

I think you are looking for the rsync command

share|improve this answer
    
I see you awarded the answer already, but you may wish to consider the extra features offered by rsync... serverfault.com/questions/43014/… – Gus Jan 27 '12 at 23:51

You must log in to answer this question.