M
M
mr_creo2017-04-06 19:19:14
linux
mr_creo, 2017-04-06 19:19:14

How to merge two directories?

How to merge two directories with replacement if they match, not by copying files, but by moving them?
The structure in the dir1 and dir2 directories is unknown and may be partially repeated.
Preferably one line.
Before

.
 |-- dir1
 |   |-- a
 |   |   |-- file1.txt (60GB)
 |   |   `-- file2.txt
 |   |-- b
 |   |   `-- file3.txt (20GB)
 |   `-- c
 |       `-- file4.txt
 `-- dir2
     |-- a
     |   |-- file1.txt (10GB)
     |   `-- file6.txt
     |-- b
     |   |-- file3.txt (50GB)
     |   `-- file8.txt
     `-- c
         |-- file10.txt
         `-- file9.txt

After
.
 |-- dir1
 `-- dir2
     |-- a
     |   |-- file1.txt (60GB)
     |   |-- file2.txt
     |   `-- file6.txt
     |-- b
     |   |-- file3.txt (20GB)
     |   `-- file8.txt
     `-- c
         |-- file4.txt
         |-- file9.txt
         `-- file10.txt

To create a structure:
mkdir test
cd test
mkdir dir1 dir1/a dir1/b dir1/c
dd if=/dev/zero of=dir1/a/file1.txt bs=1M count=60
dd if=/dev/zero of=dir1/a/file2.txt bs=1M count=1
dd if=/dev/zero of=dir1/b/file3.txt bs=1M count=20
dd if=/dev/zero of=dir1/c/file4.txt bs=1M count=1
mkdir dir2 dir2/a dir2/b dir2/c
dd if=/dev/zero of=dir2/a/file1.txt bs=1M count=10
dd if=/dev/zero of=dir2/a/file6.txt bs=1M count=1
dd if=/dev/zero of=dir2/b/file3.txt bs=1M count=50
dd if=/dev/zero of=dir2/b/file8.txt bs=1M count=1
dd if=/dev/zero of=dir2/c/file9.txt bs=1M count=1
dd if=/dev/zero of=dir2/c/file10.txt bs=1M count=1

View structure:
tree .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotkot, 2017-04-06
@mr_creo

before:

% tree -h test
test
├── [4.0K]  dir1
│   ├── [4.0K]  a
│   │   ├── [ 60M]  file1.txt
│   │   └── [1.0M]  file2.txt
│   ├── [4.0K]  b
│   │   └── [ 20M]  file3.txt
│   └── [4.0K]  c
│       └── [1.0M]  file4.txt
└── [4.0K]  dir2
    ├── [4.0K]  a
    │   ├── [ 10M]  file1.txt
    │   └── [1.0M]  file6.txt
    ├── [4.0K]  b
    │   ├── [ 50M]  file3.txt
    │   └── [1.0M]  file8.txt
    └── [4.0K]  c
        ├── [1.0M]  file10.txt
        └── [1.0M]  file9.txt

8 directories, 10 files

do not copy, but create hard links : after:
% tree -h test
test
├── [4.0K]  dir1
└── [4.0K]  dir2
    ├── [4.0K]  a
    │   ├── [ 60M]  file1.txt
    │   ├── [1.0M]  file2.txt
    │   └── [1.0M]  file6.txt
    ├── [4.0K]  b
    │   ├── [ 20M]  file3.txt
    │   └── [1.0M]  file8.txt
    └── [4.0K]  c
        ├── [1.0M]  file10.txt
        ├── [1.0M]  file4.txt
        └── [1.0M]  file9.txt

5 directories, 8 files

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question