Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Pass Find results to CP such that File Names with Spaces work [duplicate]

Tags:

bash

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 ?

like image 769
SMTF Avatar asked Dec 16 '22 06:12

SMTF


1 Answers

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/ \;
like image 69
twalberg Avatar answered Mar 30 '23 01:03

twalberg