Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say you have this directory tree

parent
   -----child1
           -----file0 
           -----grandchild1
                      -----file1
                      -----file2 
           -----grandchild2
                      -----file3
   -----child2

Now there are two things I need to learn how to do

  1. Copy the folder child1 into child2, so that there will be a parent/child2/child1/grancdhild1/file1
  2. Copy the contents of folder child1 to child2, so there will parent/child2/grancdhild1/file1

Notice the second one does not have a "child1", in the first one we are copying a folder into a new folder, in the second one we are copying the contents of a folder to another folder.

If it matters, lets say your current location is parent.

My main challenge here is to copy everything (sub directories, hidden files etc..)

share|improve this question
up vote 3 down vote accepted

Rsync to the rescue! :-)

Watch that slash character (or its absence) at the end of the 'child1' parameter. That is the only difference. The commands are executed in parent.

$ rsync -a child1 child2/

Result:

├── child1
│   ├── file0
│   ├── grandchild1
│   │   ├── file1
│   │   └── file2
│   └── grandchild2
│       └── file3
└── child2
    └── child1
        ├── file0
        ├── grandchild1
        │   ├── file1
        │   └── file2
        └── grandchild2
            └── file3

$ rsync -a child1/ child2/

Result:

├── child1
│   ├── file0
│   ├── grandchild1
│   │   ├── file1
│   │   └── file2
│   └── grandchild2
│       └── file3
└── child2
    ├── file0
    ├── grandchild1
    │   ├── file1
    │   └── file2
    └── grandchild2
        └── file3
share|improve this answer

Assuming parent is your working direcotry

To do 1: cp -r child1 child2

To do 2: cp -r child1/* child2

There are different implementations of cp in *nix world but -r switch should work. Check your documentation for details. Also take a look at -a switch in GNU cp.

share|improve this answer
3  
Note that shell globbing (here, the '*') usually won't match anything starting with a period ('.'). – Eroen Dec 13 '11 at 8:58
    
Thanks for pointing that out! I forgot to mention it. – thebodzio Dec 13 '11 at 9:24
1  
Stupid dotfiles. – Rob Dec 14 '11 at 15:57
    
So in linux, * marches directories (unlike MS DOS), but not hidden files correct? – user893730 Dec 14 '11 at 18:17
1  
It wouldn't be right to say "in linux" because "*" symbol expansion depends on specific shell. For specific details about "*" meaning in bash I must point you to bash documentation section "Pathname Extension", part about environmental variable "GLOBIGNORE" and description of "dotglob" option (they say a bit about "*" expanding to file names starting with dot). Be advised: other shells may behave differently in that matter. – thebodzio Dec 14 '11 at 23:26

To copy child1, you would do cp child1 child2/child1. To copy the contents of child1, you would do cp child1/* child2/*.

share|improve this answer
1  
Aaand, that probably just got flagged by the FBI :P – SaintWacko Dec 12 '11 at 22:47
    
Does that copy the directories inside child1? – user893730 Dec 12 '11 at 22:50
    
I believe it will. I'm at work right now, and don't have access to any Linux systems. You may have to add the -r tag for that. – SaintWacko Dec 12 '11 at 22:57
    
The -r option is needed for cp, not for mv. Although, you'd probably be better of with "cp child1 child2/" – Eroen Dec 12 '11 at 23:00
1  
@Eroen Right, but he said he wanted to copy it, not move it. – SaintWacko Dec 12 '11 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .