【问题标题】:pdftotext all files in subdirectories if it doesn't already existpdftotext 子目录中的所有文件(如果不存在)
【发布时间】:2017-10-20 13:56:47
【问题描述】:

如果文本文件尚不存在,我需要将子目录中的所有文件pdftotext。我试过了:

find . -name "*.pdf" | while read file; if [ ! -e $file.txt ] do pdftotext $file; done;

但收到:-bash: 意外标记“完成”附近的语法错误

【问题讨论】:

    标签: bash pdf while-loop find pdftotext


    【解决方案1】:

    我建议:

    find . -name "*.pdf" | while IFS= read -r file; do if [ ! -e "$file.txt" ]; then pdftotext "$file"; fi; done
    

    请参阅:help whilehelp if

    【讨论】:

      【解决方案2】:

      不要将数据通过管道传输到 shell;从 find 中执行一个shell 循环。

      script='
        for f in "$@"; do
          if ! [ -e "$f" ]; then
            pdftotext "$f"
          fi
        done
      '
      find . -name '*.pdf' -exec sh -c "$script" _ {} +
      

      这适用于任何有效的文件名,即使是包含换行符的文件名。 find 将在每次调用时将尽可能多的文件传递给脚本,并根据需要多次调用脚本来处理所有文件。

      【讨论】:

        猜你喜欢
        • 2013-07-10
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 2014-03-08
        • 2016-11-07
        相关资源
        最近更新 更多