1

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..)

3 Answers 3

3

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
2

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.

5
  • 3
    Note that shell globbing (here, the '*') usually won't match anything starting with a period ('.').
    – Eroen
    Dec 13, 2011 at 8:58
  • 1
    Stupid dotfiles.
    – Rob
    Dec 14, 2011 at 15:57
  • So in linux, * marches directories (unlike MS DOS), but not hidden files correct?
    – user893730
    Dec 14, 2011 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, 2011 at 23:26
  • 1
    @Eroen is right - * doesn't match dot files. But there's another way to copy everything (including dot files!) from child1 to child2. Just replace that * with a . (dot), as in: cp -r child1/. child2
    – gerlos
    Sep 20, 2017 at 22:11
0

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

5
  • 1
    Aaand, that probably just got flagged by the FBI :P
    – SaintWacko
    Dec 12, 2011 at 22:47
  • Does that copy the directories inside child1?
    – user893730
    Dec 12, 2011 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, 2011 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, 2011 at 23:00
  • 1
    @Eroen Right, but he said he wanted to copy it, not move it.
    – SaintWacko
    Dec 12, 2011 at 23:03

You must log in to answer this question.

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