5

I'm trying to copy files with specific attachments to a different directory with their relative paths preserved. From the original top path I am calling:

cp --parents `find . -name \*.pdf -print` /new_path/

I believe this works; however only if the file found has no spaces in the name.

I also tried:

cp --parents `find . -name \*.pdf -print0` /new_path/

This doesn't work obviously because without the new line character cp receives the wrong name.

Is it possible to surround the find result with quotes ?

SMTF
  • 705
  • 10
  • 16

2 Answers2

10

Try this:

find . -name \*.pdf -print0 | xargs -0 -n 1 -Ifoo cp --parents foo /new_path/

Or

find . -name \*.pdf -exec cp --parents {} /new_path/ \;
twalberg
  • 59,951
  • 11
  • 89
  • 84
3

Another way. This would copy all files in one call as multiple arguments to cp.

find . -name '*.pdf' -exec cp --parents -t /new_path '{}' '+'
konsolebox
  • 72,135
  • 12
  • 99
  • 105