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


当前回答

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

其他回答

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

pdfgrep -HiR 'pattern' /path

当执行pdfgrep——help时:

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

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

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

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

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

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

你的发行版应该提供一个名为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选项打印上下文行。

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

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

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

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