In Bash, how can I use cp
to copy a directory structure except those files that match one of a number of patterns, e.g. *.txt
and *~
?
I am happy to use
shopt -s extglob
if required, and have tried things like
cp -rp !(*.txt,*~) destination_folder/
but without success. In the example above, the pattern is not matched in subdirectories, and using **/!(*.txt,*~)
flattens the directory structure despite the -p option.
Ideally, subdirectories of the source folder should be created in the destination folder tree even if they are/end up empty.
If there is no way of achieving this with cp
, please suggest an alternative bash command.