linux - Bash脚本qpdf在1个目录中拆分多个pdf

标签 linux ubuntu qpdf

我编写了一个简单的 bash 脚本,因为我需要定期将多个 pdf 拆分为 2 个 pdf。我需要每次都以相同的顺序拆分它们(第 1-5 页和第 6 页到最后一页)。我需要对新拆分的 pdf 进行唯一命名,以便将它们分开(即 inv-1.pdf rec-1.pdf;inv-2.pdf rec-2.pdf 等)。我的脚本仅在文件夹中有 5 个以上时才拆分 1 个 pdf。任何建议表示赞赏。

    #!/bin/bash
    for file in $(ls REQ*.pdf)

    do
        qpdf ${file} --pages . 1-5 -- inv-%d.pdf |\
        qpdf ${file} --pages . 6-z -- rec-%d.pdf |\
        echo >> ${file}
    done
'''

最佳答案

快速解决方案:追加| xargs echo给您的ls命令。

    #!/bin/bash
    for file in $(ls REQ*.pdf | xargs echo)

    do
        qpdf ${file} --pages . 1-5 -- inv-%d.pdf |\
        qpdf ${file} --pages . 6-z -- rec-%d.pdf |\
        echo >> ${file}
    done
理由:在您的情况下,ls输出是每行一个文件名。您的 for循环仅在同一行中获取文件名。其余的被忽略。您可以阅读有关 ls 的更多信息此答案的输出行为- What is the magic separator between filenames in ls output?
管道 ( | ) 重定向 ls输出到 xargs适用echo在一行上生成以空格分隔的文件名。
阅读更多关于 xargsman page

关于linux - Bash脚本qpdf在1个目录中拆分多个pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68959589/

相关文章:

linux - 在第二个字段上加入未排序的文件

c - Maikefile错误make : *** [prepare] Error 127

c - 枚举分配给网络接口(interface)的每个 IP 地址

ruby-on-rails - Ubuntu 14.04、Ruby on Rails 和 GEOS

linux - 对于使用 libvrt 创建的 CentOS 虚拟机,需要使用 sda(而不是 vda)进行分区

python - tflearn - <类型 'exceptions.AttributeError' > : 'module' object has no attribute 'PROTOCOL_SSLv3'

mysql - 无法启动Mysql-server服务

c++ - 在 PDF 中,如果 Encoding 和 ToUnicode 都存在于 PDF 中,如何映射文本提取?

encryption - Qpdf 没有密码无法加密