如何在目录/子目录中搜索PDF文件的内容?我在找一些命令行工具。grep似乎不能搜索PDF文件。


当前回答

有一个开源的通用资源grep工具crgrep,它可以在PDF文件中搜索,也可以搜索其他资源,比如嵌套在档案中的内容、数据库表、图像元数据、POM文件依赖关系和web资源——以及这些资源的组合,包括递归搜索。

Files选项卡下的完整描述几乎涵盖了该工具支持的内容。

我开发的crgrep是一个开源工具。

其他回答

如果你想用pdftotext查看文件名,使用以下命令:

find . -name '*.pdf' -exec echo {} \; -exec pdftotext {} - \; | grep "pattern\|pdf" 

首先将所有pdf文件转换为文本文件:

for file in *.pdf;do pdftotext "$file"; done

然后像往常一样使用grep。这是特别好的,因为当您有多个查询和许多PDF文件时,它是快速的。

还有另一个实用程序叫做ripgrep-all,它是基于ripgrep的。

它不仅可以处理PDF文档,比如Office文档和电影,而且作者声称它比pdfgrep更快。

递归搜索当前目录的命令语法,第二个命令只限制PDF文件:

rga 'pattern' .
rga --type pdf 'pattern' .

我写了这个破坏性的小脚本。祝你玩得开心。

function pdfsearch()
{
    find . -iname '*.pdf' | while read filename
    do
        #echo -e "\033[34;1m// === PDF Document:\033[33;1m $filename\033[0m"
        pdftotext -q -enc ASCII7 "$filename" "$filename."; grep -s -H --color=always -i $1 "$filename."
        # remove it!  rm -f "$filename."
    done
}

使用pdfgrep:

pdfgrep -HinR 'FWCOSP' DatenModel/

在这个命令中,我在DatenModel/文件夹中搜索单词FWCOSP。

正如你在输出中看到的,你可以有文件名和行号:

我使用的选项是:

-i : Ignores, case for matching
-H : print the file name for each match
-n : prefix each match with the number of the page where it is found
-R : same as -r, but it also follows all symlinks.