Ubuntu – Copy every file in a directory structure into specific path only if file does not exist there already

bashcommand linefilesfind

Would like to copy every file from a directory structure to a specific directory only if file does not exist there already.

Got the first part from this Stack Overflow question:

find . -type f -exec cp {} /target-directory \;

How can I check if the file exists or not? If not, copy the file, otherwise skip.

Best Answer

You can use -u switch from cp command:

copy only when the SOURCE file is newer than the destination file or when the destination file is missing

or use rsync command with --ignore-existing:

skip updating files that exist on receiver

Example:

rsync --ignore-existing source/* destination/