如何搜索多个pdf文件的内容?

时间:2011-01-10 03:23:22

标签: linux pdf full-text-search grep debian

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

13 个答案:

答案 0 :(得分:191)

pdfgrep,正如其名称所暗示的那样。

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

我用它进行简单搜索,效果很好。

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

由于版本1.3.0 pdfgrep支持递归搜索。自Ubuntu 12.10(Quantal)以来,该版本在Ubuntu中可用。

答案 1 :(得分:187)

您的发行版应提供名为pdftotext的实用程序:

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

“ - ”是将pdftotext输出到stdout而不是文件的必要条件。 --with-filename--label=选项会将文件名放在grep的输出中。 可选的--color标志很好,告诉grep使用终端上的颜色输出。

(在Ubuntu中,pdftotext由包xpdf-utilspoppler-utils提供。)

如果您想使用pdftotext不{J} grep的功能,则使用pdfgrepgrep的方法优于pdfgrep支持。 注意:pdfgrep-1.3.x支持-C选项以打印上下文行。

答案 2 :(得分:25)

Recoll是一个非常出色的Unix / Linux全文GUI搜索应用程序,支持许多不同的格式,包括PDF。它甚至可以将查询的确切页码和搜索项传递给文档查看器,从而允许您直接从其GUI跳转到结果。

Recoll还带有可行的命令行界面和web-browser interface

答案 3 :(得分:12)

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

pdfgrep -HiR 'pattern' /path

执行pdfgrep --help时:

  • H:打印每场比赛的文件名。
  • i:忽略案件区别。
  • R:递归搜索目录。

它在我的Ubuntu上运行良好。

答案 4 :(得分:7)

我制作了这个破坏性小脚本。玩得开心。

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
}

答案 5 :(得分:2)

我遇到了同样的问题,因此我写了一个脚本,它搜索指定文件夹中的所有pdf文件以查找字符串,并打印与查询字符串匹配的PDF文件。

也许这会对你有所帮助。

您可以下载here

答案 6 :(得分:2)

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

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

答案 7 :(得分:2)

我喜欢@ sjr的答案,但我更喜欢xargs vs -exec。我发现xargs更加通用。例如,对于-P,我们可以在有意义的情况下利用多个CPU。

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

答案 8 :(得分:1)

有一个开源公共资源grep工具crgrep可以在PDF文件中搜索,但也可以搜索其他资源,例如嵌套在档案,数据库表,图像元数据,POM文件依赖项和Web资源中的内容 - 以及这些包括递归搜索。

“文件”选项卡下的完整说明几乎涵盖了该工具支持的内容。

我开发了crgrep作为开源工具。

答案 9 :(得分:1)

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

<dependency>
        <groupId>org.scalatest</groupId>
        <artifactId>scalatest_2.11</artifactId><!-- this was previously 2.10 -->
        <version>2.2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.scalamock</groupId>
        <artifactId>scalamock-scalatest-support_2.11</artifactId>
        <version>3.2</version>
        <scope>test</scope>
    </dependency>

然后正常使用for file in *.pdf;do pdftotext "$file"; done 。这是特别好的,因为当您有多个查询和大量PDF文件时它很快。

答案 10 :(得分:0)

您需要一些像pdf2text这样的工具才能首先将您的pdf转换为文本文件,然后在文本内部进行搜索。 (您可能会遗漏一些信息或符号)。

如果您使用的是编程语言,可能会为此目的编写pdf库。例如Perl的http://search.cpan.org/dist/CAM-PDF/

答案 11 :(得分:0)

还有一个名为ripgrep-all的实用程序,它基于ripgrep

它不仅可以处理Office文档和电影之类的PDF文档,而且对作者claims的处理要比pdfgrep快。

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

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

答案 12 :(得分:-1)

尝试在上面的一个简单脚本中使用'acroread'

相关问题