0

I am trying to copy a folder containing a subfolder structure, while excluding a specified subfolder by using the find -exec cp command. I have managed to use multiple working excluding options when I am using the find command alone, but once I add the '-exec cp' command, the excluding terms work no longer.

Imagine the directory of interest containing multiple files and subfolders, with one subfolder named "exclusion_string"

This find command works properly when used alone:

find ~/directory/of/interest/ -maxdepth 2 ! -name "*exclusion_string*"

... while this command negates the exclusion criterium:

find ~/directory/of/interest/ -maxdepth 2 ! -name "*exclusion_string*" -exec cp -r '{}' . \;

Likewise, when using other criteria or arguments, the exclusion of a subdirectory is lost, E.g:

find ~/directory/of/interest/ -maxdepth 2 -name "*" -size -100k -exec cp -r '{}' . \;

find ~/directory/of/interest/ -maxdepth 2 -name "*exclusion_string*" | xargs cp -rt .


What am I missing here?

Sam
  • 1
  • 1
  • I can't reproduce the issue, you need a [MCVE](https://stackoverflow.com/help/mcve). Maybe the excluded directories are sub-directories of one of the chosen directories? `cp -r` copies recursively – Thor Apr 29 '19 at 10:17
  • That is indeed the case; I would like to copy a folder along with its sub-directory structure while excluding one subdirectory. How can I keep the directory structure without copying recursively? – Sam May 13 '19 at 12:30
  • This would be easier with `rsync` instead of `cp`, it supports exclusion patterns as well – Thor May 13 '19 at 12:59

0 Answers0