Copying specific subfolders with directory structure to a new folder

cpfile-copyrecursive

I am having a the following directory structure:

                Main_Dir
                   |
  -----------------------------------
  Subdir1       Subdir2       Subdir3
     |             |             |
 ---------     ---------     ---------
 |   |   |     |   |   |     |   |   |            
fo1 fo2 f03   fo1 fo2 f03   fo1 fo2 f03

I want to copy all the subdirectories (Subdir1, Subdir2, Subdir3) to a new folder.
But I only want to copy fo1 and fo2 folders in the new place.

Not sure how could it be done.

Best Answer

If the tree of directories is more than just ..../f03 you can use this rsync command to copy every fo1 & fo2 and exclude every other directory with the name fo*.

$ rsync -avz --include='fo[12]/' --exclude='fo*/' \
      Main_Dir/ new_Main_Dir/.

When dealing with these types of copy scenarios I always make use of rsync and it's --dry-run & --verbose switches so I can see what it's going to do without actually having to copy the files.

$ rsync -avz --dry-run --verbose --include='fo[12]/' --exclude='fo*/' \
      Main_Dir/ new_Main_Dir/.

Example

Dry run.

$ rsync -avz --dry-run --include='fo[12]/' --exclude='fo*/' \
      Main_Dir/ new_Main_Dir/.
sending incremental file list
./

Subdir1/
Subdir1/fo1/
Subdir1/fo2/
Subdir2/
Subdir2/fo1/
Subdir2/fo2/
Subdir3/
Subdir3/fo1/
Subdir3/fo2/

sent 201 bytes  received 51 bytes  504.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

If you want to see some of rsync's internal logic as to what is being included/excluded then make use of the --verbose switch.

$ rsync -avz --dry-run --verbose --include='fo[12]/' --exclude='fo*/' \
      Main_Dir/ new_Main_Dir/.

sending incremental file list
[sender] showing directory Subdir1/fo2 because of pattern fo[12]/
[sender] showing directory Subdir1/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir1/fo3 because of pattern fo*/
[sender] showing directory Subdir2/fo2 because of pattern fo[12]/
[sender] showing directory Subdir2/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir2/fo3 because of pattern fo*/
[sender] showing directory Subdir3/fo2 because of pattern fo[12]/
[sender] showing directory Subdir3/fo1 because of pattern fo[12]/
[sender] hiding directory Subdir3/fo3 because of pattern fo*/
delta-transmission disabled for local transfer or --whole-file
./
Subdir1/
Subdir1/fo1/
Subdir1/fo2/
Subdir2/
Subdir2/fo1/
Subdir2/fo2/
Subdir3/
Subdir3/fo1/
Subdir3/fo2/
total: matches=0  hash_hits=0  false_alarms=0 data=0

sent 201 bytes  received 51 bytes  504.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

If you need to exclude other forms of directories you can add multiple excludes.

Related Question