合并数字排序的pdf文件 [英] Merge pdf files with numerical sort
问题描述
我正在尝试编写bash脚本以将目录的所有pdf文件合并为一个单独的pdf文件.命令pdfunite *.pdf output.pdf
成功实现了此目的,但是它以常规顺序合并了输入文档:
I am trying to write a bash script to merge all pdf files of a directory into one single pdf file. The command pdfunite *.pdf output.pdf
successfully achieves this but it merges the input documents in a regular order:
1.pdf
10.pdf
11.pdf
2.pdf
3.pdf
4.pdf
5.pdf
6.pdf
7.pdf
8.pdf
9.pdf
我希望文档以数字顺序合并:
while I'd like the documents to be merged in a numerical order:
1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
6.pdf
7.pdf
8.pdf
9.pdf
10.pdf
11.pdf
我想将ls -v
或sort -n
和pdfunite
混合的命令可以解决问题,但是我不知道如何将它们组合在一起.
关于如何合并数字格式的 pdf文件的任何想法?
I guess a command mixing ls -v
or sort -n
and pdfunite
would do the trick but I don't know how to combine them.
Any idea on how I could merge pdf files with a numerical sort?
推荐答案
您可以使用$()
嵌入命令结果,
所以您可以按照
you can embed the result of command using $()
,
so you can do following
$ pdfunite $(ls -v *.pdf) output.pdf
或
$ pdfunite $(ls *.pdf | sort -n) output.pdf
但是,请注意,当文件名包含特殊字符(例如空格)时,此功能将无效.
However, note that this does not work when filename contains special character such as whitespace.
在这种情况下,您可以执行以下操作:
In the case you can do the following:
ls -v *.txt | bash -c 'IFS=$'"'"'\n'"'"' read -d "" -ra x;pdfunite "${x[@]}" output.pdf'
虽然看起来有点复杂,但它的合理组合
Although it seems a little bit complicated, its just combination of
- Bash: Read tab-separated file line into array
- build argument lists containing whitespace
- How to escape single-quotes within single-quoted strings?
请注意,您不能使用xargs
,因为pdfunite
要求输入pdf作为参数的中间.
我避免使用readarray
,因为较旧的bash版本不支持它,但是如果您有较新的bash
,则可以使用它代替IFS=.. read -ra ..
.
Note that you cannot use xargs
since pdfunite
requires input pdf's as the middle of arguments.
I avoided using readarray
since it is not supported in older bash version, but you can use it instead of IFS=.. read -ra ..
if you have newer bash
.
这篇关于合并数字排序的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!