递归地(许多子目录)找到pdf文件并合并成一个pdf文件(linux,bash)

Recursively(many subdirs) find pdf files and merge into one pdf file (linux, bash)

令人惊讶的是,我在同一目录中看到了许多关于如何执行此操作的帮助页面。递归使用的那些似乎对我不起作用(下面的尝试),或者需要我不想使用的并发症,因为我不理解它们(甚至比这些更糟糕)。

总而言之,我的 pdf 散布在许多子目录中,我想遍历每一个并将这些 pdf 合并成一个大 pdf。

这些主要来自:

https://unix.stackexchange.com/questions/298031/compress-all-pdf-files-recursively

Merge / convert multiple PDF files into one PDF

第一次尝试:(效果很好 - 但仅限于目录内):

qpdf --empty --pages *.pdf -- out.pdf

at top level directory, this didn't work:

find . -type f -name "*.pdf" -exec bash -c 'qpdf --empty --pages "{}" -- merged.pdf;' {} \;

第二次尝试:

find . -type f -name "*.pdf" | while read -r file; do pdfjam "$file" -o output.pdf; done

or

touch output.pdf
find . -type f -name "*.pdf" | while read -r file; do pdfjam "$file" output.pdf -o output.pdf; done

第三次尝试:

find . -type f -name "*.pdf" -exec bash -c 'pdftk "{}" cat output "new.pdf";' {} \;

or

touch new.pdf    
find . -type f -name "*.pdf" -exec bash -c 'pdftk "{}" new.pdf cat output "new.pdf";' {} \;

第四次尝试:

python3 -m pip install --user pdftools
pdftools merge --input-dir ./top_directory --output out.pdf

  usage: pdftools [-h] [-V] <command> ...
  pdftools: error: unrecognized arguments: --input-dir

第五次尝试(似乎是最成功的,尽管输出文件只有第一个文件的页面):

 find . -type f -name "*.pdf" -exec bash -c 'gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf "{}";' {} \;

我在考虑与 find .... {} \;find .... {} + 的区别,所以我也尝试了这个,

第六次尝试:

find . -type f -name "*.pdf" -exec bash -c 'gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf ;' {}  +

生成空白页。

我很清楚我在连接文件时遇到了问题 - 可能是使用 find -exec 命令,而且各种工具都没有问题....

编辑

我想我可以做一个两步程序,

find . -name "*pdf" -exec mv {} pdfs \;
qpdf --empty --pages *.pdf -- out.pdf

但我想要一个单线,但更重要的是知道为什么我使用 find 错误...

编辑 2

我真的只想要每个文件的第一页,但这没什么大不了的。

一个非常简单的解决方案,使用 iname 而不是名称(请参阅 man find)。

我推送结果在 /tmp/ 中,如果你多次 运行 命令不会干扰。

之后你必须复制/tmp/out.pdf到你想要的地方。

  qpdf --empty --pages \
     $( find . -iname '*.pdf' 2>/dev/null ) -- /tmp/out.pdf