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.

  • Do you mean "copy all files in the source tree into a (specific) single directory with no subdirectories"? Or "copy all files in the source tree into a subdirectory of the destination directory similar to where it was in the source tree"? I read the question as the first case - can you edit the question to clarify? (I know you have your answer, but it will be read, and used, by others.) – Volker Siegel Jun 19 '17 at 23:05

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


rsync --ignore-existing source/* destination/

Your original command can be rewritten as:

find . -type f -exec bash -c 'test -e /target-directory/"$1" || cp "$1" /target-directory' sh {} \;

The key here is that we call shell with specific commands and pass found file as $1 argument. If test -e /target-directory/"$1" fails, that means file doesn't exist, in which case cp will copy the file.

In general, one can use other commands, as long as the command can verify existence of a file. Some of the other alternatives:

  • /usr/bin/realpath -e /target-directory/"$1" > /dev/null || cp "$1" /target-directory
  • stat >/dev/null /target-directory/"$1" || cp "$1" /target-directory/"$1"
  • 2
    Why stat to test for file existence in Bash? Wouldn't test -e be much simpler? – David Foerster Jun 20 '17 at 7:29
  • @DavidFoerster No particular reason. test command will do too. I'll edit my answer later if i don't forget – Sergiy Kolodyazhnyy Jun 20 '17 at 16:16

Read man cp, and use the --no-clobber option to cp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.