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?

1
  • 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 Commented Jan 28, 2012 at 21:47

3 Answers 3

6

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

5
  • This works but it's totally redundant to type folder2 in my machine. Commented Jan 28, 2012 at 21:52
  • I don't understand what do you mean by redundant? Commented Jan 28, 2012 at 21:53
  • 1
    It's not needed. Even with $cp -R parentFolder/folder2 parentFolder/folder1/folder1.1/, it works the same way. Commented Jan 28, 2012 at 21:54
  • oh, yes... indeed. But I have just set it there make sure it works for him Commented Jan 28, 2012 at 21:57
  • This is the one and only answer! Commented Jan 28, 2012 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/
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.

1
  • 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/. Commented Jan 28, 2012 at 23:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.