216

I have script:

find ./SourceFolder/ -maxdepth 4 -exec cp -R '{}' ./DestFolder/ \;

SourceDir contains also sub-folders.

Problem that in DestFolder not only all tree, but in up level all another levels and files.

How to fix ?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
user710818
  • 23,228
  • 58
  • 149
  • 207

4 Answers4

412
cp -r ./SourceFolder ./DestFolder
lanzz
  • 42,060
  • 10
  • 89
  • 98
  • 11
    So, to clarify, capital -R option will copy the root dir again; small -r option keeps the root paths the same. – AnneTheAgile Aug 07 '14 at 13:27
  • 10
    @AnneTheAgile - from my tests just now and according to the man pages, -r and -R don't differ. – aaaaaa Jan 25 '15 at 02:54
  • @aaaaaa, ty! which OS/version are you on? I found out later it seems that bash commands can differ subtly across operating systems. – AnneTheAgile Jan 25 '15 at 17:57
  • 3
    `cp` is not a bash command but a separate executable, so it is system-specific. True bash commands on the other hand are the same across operating systems. – lanzz Jan 25 '15 at 18:34
  • 1
    I had to add a slash at the end of my source folder. Not sure why. I was using build vars too. `cp -r "$CONFIG_PATH/" "$CODESIGNING_FOLDER_PATH"` – Marty Oct 17 '20 at 20:49
  • 3
    Will this not create `SourceFolder` _in_ `DestFolder`? – kontur Jan 14 '22 at 08:37
74

code for a simple copy.

cp -r ./SourceFolder ./DestFolder

code for a copy with success result

cp -rv ./SourceFolder ./DestFolder

code for Forcefully if source contains any readonly file it will also copy

cp -rf ./SourceFolder ./DestFolder

for details help

cp --help
zviad
  • 586
  • 7
  • 18
shamimiceewu025
  • 841
  • 6
  • 3
12

also try this cp -r ./dist/* ./out;

this command will copy dist/* files to out dir;

dbcookie
  • 161
  • 1
  • 4
  • 1
    @SherylHohman It's different because he put a /* on the end of the source folder. I don't know why that matters though. – Lee Meador May 11 '22 at 18:15
  • 1
    this response instead of copying the entire folder copies content of the source folder into the destination (./out) folder – user1275513 Oct 11 '22 at 06:21
3

You might find it handy to keep your attributes set

cp -arf ./SourceFolder ./DestFolder

Harry Bosh
  • 3,611
  • 2
  • 36
  • 34
  • 1
    `cp -arf ...` throws the error "cp: the -R and -r options may not be specified together." Changing it to `cp -af ...` solves it. I'm not sure if it's a typo on your end of if `cp -arf ...` actually worked for you, but I hope this helps in case anyone is getting the same error. Reference: https://stackoverflow.com/a/32695418/5810737 – GlyphCat Dec 25 '21 at 11:05