How do you distinguish a directory, and it's contents in linux?

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

user893730

Posted 2011-12-12T22:42:15.800

Reputation: 1 097

Answers

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

karatedog

Posted 2011-12-12T22:42:15.800

Reputation: 809

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.

thebodzio

Posted 2011-12-12T22:42:15.800

Reputation: 430

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 – 2017-09-20T22:11:46.623

3Note that shell globbing (here, the '*') usually won't match anything starting with a period ('.'). – Eroen – 2011-12-13T08:58:06.257

1Stupid dotfiles. – Rob – 2011-12-14T15:57:15.440

So in linux, * marches directories (unlike MS DOS), but not hidden files correct? – user893730 – 2011-12-14T18:17:55.053

1It 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 – 2011-12-14T23:26:03.550

0

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

SaintWacko

Posted 2011-12-12T22:42:15.800

Reputation: 1 482

1Aaand, that probably just got flagged by the FBI :P – SaintWacko – 2011-12-12T22:47:14.510

Does that copy the directories inside child1? – user893730 – 2011-12-12T22:50:24.787

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 – 2011-12-12T22:57:18.123

The -r option is needed for cp, not for mv. Although, you'd probably be better of with "cp child1 child2/" – Eroen – 2011-12-12T23:00:14.920

1@Eroen Right, but he said he wanted to copy it, not move it. – SaintWacko – 2011-12-12T23:03:46.067