1

I have two folders, and I would like to do:

 cp -R folder1/ folder2/

So that I all the sub folders missing in folder2 will be created, and there files copied. And all the folders in folder2 that are missing from folder1 stay the same.

2
cp -R folder1/. folder2/

You can use folder1/* instead, if you don't mind that * will not match "hidden" files which begin with a . like .bash_profile.

If there are files in folder2 with the same name as files in folder1, they will be overwritten without prompting or warning.

  • and the files that only exists in folder2 wont be deleted ? – Bionix1441 Feb 23 '17 at 16:12
  • 1
    no. cp doesn't delete files. (rsync does if you pass --delete). – sourcejedi Feb 23 '17 at 17:22
2
cp -fR folder1/ folder2/

the -f flag forces the dest file to be deleted if cannot be opened

see man page for cp

Also, another option would be to use rsync, see this answer for example:

How to overwrite target files with mv?

1

Don't use cp, instead use mv, Because: cp copy all files and it spreads time and I/O, But mv only change inode: So:

mv -f dir1/ dir2

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.