0

let assume, I have two folders; folder_1 and folder_2. There are another 24 folders in each folder (folder_1 and folder_2). Each sub-folder of folder_1 contains 1 text file whose name is geomagnetic_script_xxxx. xxxx is variable numeric, "geomagnetic_script" is constant for all folders. Sub-folders of folder_2 are empty. I need to copy each text file (geomagnetic_script_xxxx) into the sub-folder of folder_2. In this way each sub-folder of folder_2 contains 1 text file which is identical to sub-folders of folder_1.

Which script allow me to do that?

2

Run this command in the folder containing folder_1 and folder2:

find folder_1 -name 'geomagnetic_script_????' -type f -print0 | while read -d '' -r file; do echo cp "$file" folder_2/"${file#*/}"; done

Just for safety I inhibited the actual cp command by prepending echo to it. See if it's doing what you want and then remove the echo and run it again.

Sample output on my machine:

$ tree
.
├── folder_1
│   ├── subfolder_1
│   │   └── file_25406
│   ├── subfolder_10
│   │   └── file_1439
│   ├── subfolder_11
│   │   └── file_21028
│   ├── subfolder_12
│   │   └── file_13026
│   ├── subfolder_13
│   │   └── file_3717
│   ├── subfolder_14
│   │   └── file_9825
│   ├── subfolder_15
│   │   └── file_14953
│   ├── subfolder_16
│   │   └── file_12760
│   ├── subfolder_17
│   │   └── file_8490
│   ├── subfolder_18
│   │   └── file_14577
│   ├── subfolder_19
│   │   └── file_11999
│   ├── subfolder_2
│   │   └── file_24557
│   ├── subfolder_20
│   │   └── file_27957
│   ├── subfolder_21
│   │   └── file_158
│   ├── subfolder_22
│   │   └── file_1361
│   ├── subfolder_23
│   │   └── file_27370
│   ├── subfolder_24
│   │   └── file_4919
│   ├── subfolder_3
│   │   └── file_30149
│   ├── subfolder_4
│   │   └── file_5352
│   ├── subfolder_5
│   │   └── file_27907
│   ├── subfolder_6
│   │   └── file_14716
│   ├── subfolder_7
│   │   └── file_1989
│   ├── subfolder_8
│   │   └── file_7850
│   └── subfolder_9
│       └── file_31831
├── folder_2
│   ├── subfolder_1
│   ├── subfolder_10
│   ├── subfolder_11
│   ├── subfolder_12
│   ├── subfolder_13
│   ├── subfolder_14
│   ├── subfolder_15
│   ├── subfolder_16
│   ├── subfolder_17
│   ├── subfolder_18
│   ├── subfolder_19
│   ├── subfolder_2
│   ├── subfolder_20
│   ├── subfolder_21
│   ├── subfolder_22
│   ├── subfolder_23
│   ├── subfolder_24
│   ├── subfolder_3
│   ├── subfolder_4
│   ├── subfolder_5
│   ├── subfolder_6
│   ├── subfolder_7
│   ├── subfolder_8
│   └── subfolder_9
└── script.sh

50 directories, 25 files
$ find folder_1 -type f -name 'file_*' -print0 | while read -d '' -r file; do cp "$file" folder_2/"${file#*/}"; done
$ tree
.
├── folder_1
│   ├── subfolder_1
│   │   └── file_25406
│   ├── subfolder_10
│   │   └── file_1439
│   ├── subfolder_11
│   │   └── file_21028
│   ├── subfolder_12
│   │   └── file_13026
│   ├── subfolder_13
│   │   └── file_3717
│   ├── subfolder_14
│   │   └── file_9825
│   ├── subfolder_15
│   │   └── file_14953
│   ├── subfolder_16
│   │   └── file_12760
│   ├── subfolder_17
│   │   └── file_8490
│   ├── subfolder_18
│   │   └── file_14577
│   ├── subfolder_19
│   │   └── file_11999
│   ├── subfolder_2
│   │   └── file_24557
│   ├── subfolder_20
│   │   └── file_27957
│   ├── subfolder_21
│   │   └── file_158
│   ├── subfolder_22
│   │   └── file_1361
│   ├── subfolder_23
│   │   └── file_27370
│   ├── subfolder_24
│   │   └── file_4919
│   ├── subfolder_3
│   │   └── file_30149
│   ├── subfolder_4
│   │   └── file_5352
│   ├── subfolder_5
│   │   └── file_27907
│   ├── subfolder_6
│   │   └── file_14716
│   ├── subfolder_7
│   │   └── file_1989
│   ├── subfolder_8
│   │   └── file_7850
│   └── subfolder_9
│       └── file_31831
├── folder_2
│   ├── subfolder_1
│   │   └── file_25406
│   ├── subfolder_10
│   │   └── file_1439
│   ├── subfolder_11
│   │   └── file_21028
│   ├── subfolder_12
│   │   └── file_13026
│   ├── subfolder_13
│   │   └── file_3717
│   ├── subfolder_14
│   │   └── file_9825
│   ├── subfolder_15
│   │   └── file_14953
│   ├── subfolder_16
│   │   └── file_12760
│   ├── subfolder_17
│   │   └── file_8490
│   ├── subfolder_18
│   │   └── file_14577
│   ├── subfolder_19
│   │   └── file_11999
│   ├── subfolder_2
│   │   └── file_24557
│   ├── subfolder_20
│   │   └── file_27957
│   ├── subfolder_21
│   │   └── file_158
│   ├── subfolder_22
│   │   └── file_1361
│   ├── subfolder_23
│   │   └── file_27370
│   ├── subfolder_24
│   │   └── file_4919
│   ├── subfolder_3
│   │   └── file_30149
│   ├── subfolder_4
│   │   └── file_5352
│   ├── subfolder_5
│   │   └── file_27907
│   ├── subfolder_6
│   │   └── file_14716
│   ├── subfolder_7
│   │   └── file_1989
│   ├── subfolder_8
│   │   └── file_7850
│   └── subfolder_9
│       └── file_31831
└── script.sh

50 directories, 49 files
$ 

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.