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


当前回答

我的实际版本的pdfgrep(1.3.0)允许以下:

pdfgrep -HiR 'pattern' /path

当执行pdfgrep——help时:

H:打印每个匹配项的文件名。 i:忽略大小写区别。 R:递归搜索目录。

它在我的Ubuntu上运行得很好。

其他回答

我喜欢@sjr的答案,但我更喜欢xargs vs -exec。我发现xargs更通用。例如,使用-P,我们可以在必要时利用多个cpu。

find . -name '*.pdf' | xargs -P 5 -I % pdftotext % - | grep --with-filename --label="{}" --color "pattern"

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

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

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

你的发行版应该提供一个名为pdftotext的实用程序:

find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' \;

如果要将pdftotext输出到标准输出,而不是输出到文件,则必须使用“-”。 ——with-filename和——label=选项将把文件名放在grep的输出中。 可选的——color标志很好,它告诉grep在终端上使用颜色输出。

(在Ubuntu中,pdftotext是由xpdf-utils或poppler-utils包提供的。)

如果您想使用GNU grep中pdfgrep不支持的特性,这种使用pdftotext和grep的方法比pdfgrep更有优势。注意:pdfgrep - 1.3。x支持-C选项打印上下文行。

你需要一些工具,如pdf2text,首先将pdf转换为文本文件,然后在文本中搜索。(您可能会错过一些信息或符号)。

如果你正在使用一种编程语言,很可能有专门为此目的编写的pdf库。例如:http://search.cpan.org/dist/CAM-PDF/ for Perl

使用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.