1

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?

  • 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
6

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

| improve this answer | |
  • This works but it's totally redundant to type folder2 in my machine. – Abhijeet Rastogi 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. – Abhijeet Rastogi 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 Mokaddim Jan 28 '12 at 22:01
1

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/
| improve this answer | |
1

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.

| 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 'SO- stop being evil' Jan 28 '12 at 23:24

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.