Take the 2-minute tour ×

How could I search the contents of PDF files in a directory/subdirectory? I am looking for some command line tools. It seems that grep can't search PDF files.

share|improve this question
Grep will not work as PDF is a binary format and the text is often compressed or encoded in a variety of ways. –  mark stephens Jan 10 '11 at 7:37
Here is a GUI solution: Adobe Reader, see wikispaces.psu.edu/display/training/… –  moose Aug 1 '12 at 13:44
Related: unix.stackexchange.com/questions/6704/grep-pdf-files –  Flow Jun 22 '13 at 12:59
Adobe reader works fine, but it does not index; so if you have a lot of files, it will be slow. Any indexing solution? –  Таня Т. Jan 29 at 16:40
add comment

11 Answers

up vote 57 down vote accepted

Your distribution should provide a utility called pdftotext:


The "-" is necessary to have pdftotext output to stdout, not to files. The --with-filename and --label= options will put the file name in the output of grep. The optional --color flag is nice and tells grep to output using colors on the terminal.

(In Ubuntu, pdftotext is provided by the package xpdf-utils or poppler-utils.)

This method, using pdftotext and grep, has an advantage over pdfgrep if you want to use features of GNU grep that pdfgrep doesn't support. For instance, you can add the -C5 option before "your pattern" to include 5 lines of context to the output -- pdfgrep does not support this.

share|improve this answer
This works, but does't show matching filenames. Any idea? –  Fabian Thommen Nov 30 '12 at 16:35
@Kurt Pfeifle The edit "(Edit by -kp-)" you made does no not work since grep filters the printed file names. –  Raphael Ahrens Aug 13 '13 at 9:07
@sjr no, while the pdfgrep solution is good for really quick and simple searches, often I want to get some context, as a single line won't be helpful enough -- so as I added to this answer: For instance, you can add the -C5 option before "your pattern" to include 5 lines of context to the output -- pdfgrep does not support this –  Colin D Bennett Oct 14 '13 at 18:58
oh that's cool, glad to know there are advantages to this even though it is much less obvious to most people wtf it is doing –  sjr Oct 16 '13 at 4:30
@sjr Just for the record: I am using Ubuntu 12.10 and pdfgrep is useless, it reports a tremendous amount of rubbish on files it cannot handle. Your solution on the other hand helped. So please don't delete it, even after 3 years it is still helpful! –  Ali Jun 12 at 21:13
show 1 more comment

There is pdfgrep, which does exactly what its name suggests.

I've used it for simple searches and it worked fine.

(There are packages in Debian, Ubuntu and Fedora.)

Since version 1.3.0 pdfgrep supports recursive search. This version is available in Ubuntu since Ubuntu 12.10 (Quantal).

share|improve this answer
Note that unlike grep, pdfgrep does not have a recursive search option (-r), so it will still need to be used with find or other commands to search directory hierarchies. For example: –  pavon Apr 10 '12 at 14:16
find /path -iname '*.pdf' -exec pdfgrep -H 'pattern' {} \; –  pavon Apr 10 '12 at 14:23
From Natty (Ubuntu 11.04) upwards (See packages.ubuntu.com/…) –  moose Aug 1 '12 at 13:34
this work for me, show the title of book. Thanks. –  Guillermo Tobar Oct 17 '12 at 19:19
Great tool. Works for me. –  Jingguo Yao Mar 15 '13 at 6:29
add comment

Recoll is a fantastic full-text GUI search application for Unix/Linux that supports dozens of different formats, including PDF. It can even pass the exact page number and search term of a query to the document viewer and thus allows you to jump to the result right from its GUI.

Recoll also comes with a viable command-line interface and a web-browser interface.

share|improve this answer
Tested, works fine. Thanks –  Leszek Żarna Oct 26 '13 at 8:06
add comment

I made this destructive small script. Have fun with it.

function pdfsearch()
    find . -iname '*.pdf' | while read filename
        #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."
share|improve this answer
+1. But instead of the $filename. you should pipe it into grep. –  Raphael Ahrens Aug 13 '13 at 9:06
add comment

I had the same problem and thus I wrote a script which searches all pdf files in the specified folder for a string and prints the PDF files wich matched the query string.

Maybe this will be helpful to you.

You can download it here

share|improve this answer
add comment

recommend pdfgrep( Pdfgrep is a tool to search text in PDF files. It works similar to grep. ) and if you want windows version, please visit here, http://soft.rubypdf.com/software/pdfgrep-windows-version

share|improve this answer
add comment

If You want to see file names with pdftotext use following command:

find . -name '*.pdf' -exec echo {} \; -exec pdftotext {} - \; | grep "pattern\|pdf" 
share|improve this answer
This also displays file name without the pattern but it is useful.. –  Raghav Jul 17 '13 at 19:09
add comment

You need some tools like pdf2text to first convert your pdf to a text file and then search inside the text. (You will probably miss some information or symbols).

If you are using a programming language there are probably pdf libraries written for this purpose. e.g. http://search.cpan.org/dist/CAM-PDF/ for Perl

share|improve this answer
add comment

try using 'acroread' in a simple script like the one above

share|improve this answer
This is really a comment, not an answer to the question. You can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Florent Aug 17 '12 at 9:05
add comment

There is an open source common resource grep tool crgrep which searches within PDF files but also other resources like content nested in archives, database tables, image meta-data, POM file dependencies and web resources - and combinations of these including recursive search.

The full description under the Files tab pretty much covers what the tool supports.

I developed crgrep as an opensource tool.

share|improve this answer
Craig - do you have a connection to that project? If so, you should state it in your answer. I say this because you've just posted a virtually identical answer to two other old questions ... –  Stephen C Nov 10 '13 at 7:19
Updated post to clarify that I'm the author of crgrep –  Craig Apr 13 at 22:29
add comment

My actual version of pdfgrep (1.3.0) allows the following:

pdfgrep -HiR 'pattern' /path

When doing pdfgrep --help:

  • H: Print the file name for each match.
  • i: Ignore case distinctions.
  • R: Search directories recursively.

It works well on my Ubuntu.

share|improve this answer
add comment

protected by Ali Jun 12 at 21:10


Would you like to answer one of these unanswered questions instead?
