1

Suppose I have the following structure:

Folder-A
    File-1
    File-2
    File-4
    Folder-A-1
        File-5
        File-6
    Folder-A-2
        File-7

Folder-B
    File-1
    File-2
    File-3
    Folder-A-1
        File-5

I want to copy Folder-A onto / on top of Folder-B so that, logically, the result is this:

Folder-B
    File-1 *
    File-2 *
    File-3
    File-4 +
    Folder-A-1
        File-5 *
        File-6 +
    Folder-A-2 +
        File-7 +

Where * is an overwrite and + is an addition.

In other words, recursively, if the file (or folder) exists in the same path, overwrite it, and if it's new, add it.

How would I do this?

1

That's the default behavior of cp. So all you need to do is a regular recursive cp:

cp -r Folder-A/* Folder-B/

To demonstrate:

$ tree
.
├── Folder-A
│   ├── File-1
│   ├── File-2
│   ├── File-4
│   ├── Folder-A-1
│   │   ├── File-5
│   │   └── File-6
│   └── Folder-A-2
│       └── File-7
└── Folder-B
    ├── File-1
    ├── File-2
    ├── File-3
    └── Folder-A-1
        └── File-5

$ cp -r Folder-A/* Folder-B
$ tree
.
├── Folder-A
│   ├── File-1
│   ├── File-2
│   ├── File-4
│   ├── Folder-A-1
│   │   ├── File-5
│   │   └── File-6
│   └── Folder-A-2
│       └── File-7
└── Folder-B
    ├── File-1
    ├── File-2
    ├── File-3
    ├── File-4
    ├── Folder-A-1
    │   ├── File-5
    │   └── File-6
    └── Folder-A-2
        └── File-7

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.