246

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 ?

2
  • Sorry, forgot to add -name "*.txt" Commented Nov 8, 2011 at 18:58
  • 1
    You should add the missing info to your question, by editing your question Commented Feb 17, 2022 at 4:50

4 Answers 4

459
cp -r ./SourceFolder ./DestFolder
Sign up to request clarification or add additional context in comments.

7 Comments

So, to clarify, capital -R option will copy the root dir again; small -r option keeps the root paths the same.
@AnneTheAgile - from my tests just now and according to the man pages, -r and -R don't differ.
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.
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"
Will this not create SourceFolder in DestFolder?
|
80

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

Comments

20

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

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

2 Comments

@SherylHohman It's different because he put a /* on the end of the source folder. I don't know why that matters though.
this response instead of copying the entire folder copies content of the source folder into the destination (./out) folder
6

You might find it handy to keep your attributes set

cp -arf ./SourceFolder ./DestFolder

1 Comment

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: stackoverflow.com/a/32695418/5810737

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.