Skip to main content
added 202 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

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.

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
}

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.

Post Undeleted by Stéphane Chazelas
deleted 402 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With the GNU implementation ofIn cpzsh which, you seem to be using with that GNU-specific --backupcould do:

(set -o extendedglob
for file cd( usr$SEARCHDIR/search**/*.(#i)pdf(ND^/) &&) find{
 . -inamedest=$OUTPUTFOLDER/${file:t2}
 '*.pdf' !mkdir -type dp -exec \
 - $dest:h && cp --backup=t --parents -t /destination {}$file +$dest
)}

I'm assuming you don't want to copy directories even if their name ends in .pdf, so I've taken out the -r and added a ! -type d.

In any case, if you had to use xargs (not needed here as the -exec ... {} + alternative is standard), you'd use find ... -print0 | xargs -r0 ..., not -d '\n' on NL-delimited output which is neither portable nor reliable.

With the GNU implementation of cp which you seem to be using with that GNU-specific --backup:

(
  cd usr/search && find . -iname '*.pdf' ! -type d -exec \
    cp --backup=t --parents -t /destination {} +
)

I'm assuming you don't want to copy directories even if their name ends in .pdf, so I've taken out the -r and added a ! -type d.

In any case, if you had to use xargs (not needed here as the -exec ... {} + alternative is standard), you'd use find ... -print0 | xargs -r0 ..., not -d '\n' on NL-delimited output which is neither portable nor reliable.

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
}
Post Deleted by Stéphane Chazelas
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With the GNU implementation of cp which you seem to be using with that GNU-specific --backup:

(
  cd usr/search && find . -iname '*.pdf' ! -type d -exec \
    cp --backup=t --parents -t /destination {} +
)

I'm assuming you don't want to copy directories even if their name ends in .pdf, so I've taken out the -r and added a ! -type d.

In any case, if you had to use xargs (not needed here as the -exec ... {} + alternative is standard), you'd use find ... -print0 | xargs -r0 ..., not -d '\n' on NL-delimited output which is neither portable nor reliable.