3

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.

3 Answers3

5
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?

impalle
  • 304
3
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.

sourcejedi
  • 50,249
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
PersianGulf
  • 10,850