What would be a efficient way to look into two different directories and if subdirectories match, copy these subfolders in a new output folder linux or bash scripting? I know I need cp command and do match based on SC#### values.
Example folder one:
[NAME]$ Project
Sample_SC1234-AAA-AAA
Sample_SC2345-AAA-BBB
Sample_SC3456-CCC-CCC
Sample_SC4567-DDD-AAA
Example folder Two:
[NAME]$ Lz
Sample_SC1234-AAA-BBB
Sample_SC4567-BBB-AAA
Sample_SC5678-DDD-BBB
Sample_SC6789-BBB-DDD
Wanted output:
[NAME]$ New
Sample_SC1234-AAA-BBB
Sample_SC4567-BBB-AAA
Sample_SC1234-AAA-AAA
Sample_SC4567-DDD-AAA
CodePudding user response:
ls Project Lz|grep Sample_SC |cut -d '-' -f 1|sort |uniq -c |awk '{if($1 > 1)print $2}' |while read line
do
cp Project/$line* Lz/$line* New/
done
CodePudding user response:
Get the duplicate SC####
values from the directories listed under ./Project
and ./Lz
subdirectories and use those values in your recursive copy command.
#!/bin/bash
mkdir -p ./New
while read -r line ; do
cp -r ./Project/*"$line"* ./Lz/*"$line"* ./New
done < <(awk 'a[$0] {print $0}' <(grep -o 'SC[0-9]\{4\}' <(ls Lz Project)))