0

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.

John S.
  • 501
  • 2
  • 6
  • 17
  • 1
    http://stackoverflow.com/questions/4585929/how-to-use-cp-command-to-exclude-a-specific-directory#14789400 – Hunter McMillen Mar 22 '17 at 13:14
  • What is the error you are seeing? – Inian Mar 22 '17 at 13:19
  • @triplee: it is not a duplicate of the existing question. The problem is with why the `extglob` done fails for that? The duplicated question just provides alternate ways to do that and not answering the question. Posted to reopen – Inian Mar 22 '17 at 13:23
  • @Inian: There is no error, I simply haven't found a solution that does what I want. – John S. Mar 22 '17 at 14:03
  • @JohnS.: Can you try `cp -rp -- !(*.txt,*~) destination_folder/` – Inian Mar 22 '17 at 14:04
  • @HunterMcMillen, tripleee: My question is not a duplicate. Please unmark. – John S. Mar 22 '17 at 14:06
  • @Inian: Your suggestion with -- didn't work, I'm afraid. In fact, everything got copied, even a *.txt file the top-level folder. – John S. Mar 22 '17 at 14:09
  • @JohnS.: did you enable `shopt -s extglob` before running it. I thought you already did that – Inian Mar 22 '17 at 14:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138749/discussion-between-john-s-and-inian). – John S. Mar 22 '17 at 15:55

0 Answers0