0

I have a lot of different folders with pdf files and other extensions. I want to copy all the pdf files to another destination. But if I do that with find like this:

"${SEARCHDIR}" -iname "*.pdf" | xargs -d "\n" -I {} cp -r --backup=t {} "${OUTPUTFOLDER}/"

I get all the pdf files in single destination and that is too messy for me. For that reason I want to copy the matching files including the parent directory but WITHOUT the non-matching files in that directory.

Input:

user/search/test/sub
user/search/test/sub2
user/search/test/file.pdf
user/search/test/file.doc
user/search/test2/sub
user/search/test2/sub2/file2.pdf
user/search/test2/file3.pdf
user/search/test2/file.doc

Should be copied as

/destination/test/file.pdf
/destination/sub2/file2.pdf
/destination/test2/file3.pdf

EDIT: I don't want to copy the whole path. I don't want my output to be like this:

/destination/test/file.pdf
/destination/test2/sub2/file2.pdf
/destination/test2/file3.pdf

How to do this?

1 Answer 1

1

In zsh, you could do:

set -o extendedglob
for file ( $SEARCHDIR/**/*.(#i)pdf(ND^/) ) {
  dest=$OUTPUTFOLDER/${file:t2}
  mkdir -p -- $dest:h && cp --backup=t -- $file $dest
}

Where ${file:t2} gets you the two-component tail of the file, that is the file name and that of its parent directory. Then we create the head of that path as a directory and copy the file there.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.