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


当前回答

还有pdfgrep,它做的正是它的名字所暗示的。

pdfgrep -R 'a pattern to search recursively from path' /some/path

我用它做过简单的搜索,效果很好。

(Debian、Ubuntu和Fedora中都有软件包。)

从1.3.0版本开始,pdfgrep支持递归搜索。这个版本从Ubuntu 12.10 (Quantal)开始在Ubuntu中可用。

其他回答

还有pdfgrep,它做的正是它的名字所暗示的。

pdfgrep -R 'a pattern to search recursively from path' /some/path

我用它做过简单的搜索,效果很好。

(Debian、Ubuntu和Fedora中都有软件包。)

从1.3.0版本开始,pdfgrep支持递归搜索。这个版本从Ubuntu 12.10 (Quantal)开始在Ubuntu中可用。

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

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

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

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

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

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

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

试着在一个简单的脚本中使用'acroread',就像上面那样