The best option is to use globs and cp
with its --parents
option. In your case:
cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
Look:
gniourf@somewhere$ mkdir -pv Main_Dir/Subdir{1..3}/f{o{1,2},03}
mkdir: created directory `Main_Dir'
mkdir: created directory `Main_Dir/Subdir1'
mkdir: created directory `Main_Dir/Subdir1/fo1'
mkdir: created directory `Main_Dir/Subdir1/fo2'
mkdir: created directory `Main_Dir/Subdir1/f03'
mkdir: created directory `Main_Dir/Subdir2'
mkdir: created directory `Main_Dir/Subdir2/fo1'
mkdir: created directory `Main_Dir/Subdir2/fo2'
mkdir: created directory `Main_Dir/Subdir2/f03'
mkdir: created directory `Main_Dir/Subdir3'
mkdir: created directory `Main_Dir/Subdir3/fo1'
mkdir: created directory `Main_Dir/Subdir3/fo2'
mkdir: created directory `Main_Dir/Subdir3/f03'
gniourf@somewhere$ tree Main_Dir/
Main_Dir/
|-- Subdir1
| |-- f03
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- f03
| |-- fo1
| `-- fo2
`-- Subdir3
|-- f03
|-- fo1
`-- fo2
12 directories, 0 files
gniourf@somewhere$ mkdir Destination
gniourf@somewhere$ cp -nvr --parents -- Main_Dir/*/fo{1,2} Destination/
Main_Dir -> Destination/Main_Dir
Main_Dir/Subdir1 -> Destination/Main_Dir/Subdir1
`Main_Dir/Subdir1/fo1' -> `Destination/Main_Dir/Subdir1/fo1'
Main_Dir/Subdir2 -> Destination/Main_Dir/Subdir2
`Main_Dir/Subdir2/fo1' -> `Destination/Main_Dir/Subdir2/fo1'
Main_Dir/Subdir3 -> Destination/Main_Dir/Subdir3
`Main_Dir/Subdir3/fo1' -> `Destination/Main_Dir/Subdir3/fo1'
`Main_Dir/Subdir1/fo2' -> `Destination/Main_Dir/Subdir1/fo2'
`Main_Dir/Subdir2/fo2' -> `Destination/Main_Dir/Subdir2/fo2'
`Main_Dir/Subdir3/fo2' -> `Destination/Main_Dir/Subdir3/fo2'
gniourf@somewhere$ tree Destination
Destination
`-- Main_Dir
|-- Subdir1
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- fo1
| `-- fo2
`-- Subdir3
|-- fo1
`-- fo2
10 directories, 0 files
gniourf@somewhere$ # Done!
Make sure you use the --parent
option with the -r
switch. The -v
option I used is so that the command is verbose (it shows what it's doing) and the -n
option is for no clobber so as to not overwrite otherwise existing files (I use this one very often!).
If you don't want the Main_Dir
to be in the directory, do it from within the Main_Dir directly:
gniourf@somewhere$ mkdir -p Main_Dir/Subdir{1..3}/f{o{1,2},03} Destination
gniourf@somewhere$ cd Main_Dir/
gniourf@somewhere$ cp -nvr --parents -- */fo{1,2} ../Destination
Subdir1 -> ../Destination/Subdir1
`Subdir1/fo1' -> `../Destination/Subdir1/fo1'
Subdir2 -> ../Destination/Subdir2
`Subdir2/fo1' -> `../Destination/Subdir2/fo1'
Subdir3 -> ../Destination/Subdir3
`Subdir3/fo1' -> `../Destination/Subdir3/fo1'
`Subdir1/fo2' -> `../Destination/Subdir1/fo2'
`Subdir2/fo2' -> `../Destination/Subdir2/fo2'
`Subdir3/fo2' -> `../Destination/Subdir3/fo2'
gniourf@somewhere$ cd ..
gniourf@somewhere$ tree Destination/
Destination/
|-- Subdir1
| |-- fo1
| `-- fo2
|-- Subdir2
| |-- fo1
| `-- fo2
`-- Subdir3
|-- fo1
`-- fo2
9 directories, 0 files
gniourf@somewhere$