Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Let's say I have folder structure like this:

/parentFolder/
  folder1/
    folder1.1/
      file1
  folder2/
    file2

How do I copy folder2 inside folder1.1 so that file2 is also in

/parentFolder/folder1/folder1.1/folder2/file2

ie so that the folder structure looks like this:

/parentFolder/
  folder1/
    folder1.1/
      folder2/
        file2
      file1
  folder2/
    file2

?

If I just go cp -R /parentFolder/folder2 /parentFolder/folder1/folder1.1 then it copies the content of the folder2, but I want to copy folder2 as well.

Related question: would the answer applies to mv as well?

share|improve this question

migrated from stackoverflow.com Jan 28 '12 at 22:01

This question came from our site for professional and enthusiast programmers.

2  
What kind of Linux are using? from what i know it takes this cp -R /parentFolder/folder2/* /parentFolder/folder1/folder1.1 to move only the content –  mugur Jan 28 '12 at 21:47
add comment

3 Answers 3

up vote 5 down vote accepted

cp -R parentFolder/folder2 parentFolder/folder1/folder1.1/folder2

share|improve this answer
    
This works but it's totally redundant to type folder2 in my machine. –  shadyabhi Jan 28 '12 at 21:52
    
I don't understand what do you mean by redundant? –  w0rldart Jan 28 '12 at 21:53
1  
It's not needed. Even with $cp -R parentFolder/folder2 parentFolder/folder1/folder1.1/, it works the same way. –  shadyabhi Jan 28 '12 at 21:54
    
oh, yes... indeed. But I have just set it there make sure it works for him –  w0rldart Jan 28 '12 at 21:57
    
This is the one and only answer! –  Shiplu Jan 28 '12 at 22:01
add comment

Which cp are you using? UNIX tools are sort of similar among different systems, but may behave differently in some situations.

GNU cp has the behavior you want. BSD cp has the behavior you describe. Try removing the slash from the first argument, and writing:

cp -R /parentFolder/folder2 /parentFolder/folder1/folder1.1

rather than:

cp -R /parentFolder/folder2/ /parentFolder/folder1/folder1.1

According to wikipedia(1), this is how BSD cp does what you want.

share|improve this answer
1  
You've got that backwards, appending a slash causes the contents of the directory to be copied. hobbes3 must use …/folder2 and not …/folder2/. –  Gilles Jan 28 '12 at 23:24
    
oh, I really did. Thanks for noticing! –  njsg Jan 29 '12 at 0:23
add comment

I prefer to use rsync for this kind of action. On top of copying the files it can copy permissions and improve the transfer speed over conventional cp.

The trailing / on the destination is crucial for your requirement.

Try something like:

rsync -aHSv /parentFolder/folder2 /parentFolder/folder1/
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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