Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to search pdf files using the power of grep, without converting to text first in Ubuntu?

share|improve this question
I think you need to parse it thou pdf2text in order to get some usable results back... – Johan Jan 31 '11 at 14:29
For people comming here via search: If you are willing to convert it first to text files, have a look at How to search contents of multiple pdf files? – Martin Thoma Jan 2 at 22:09

12 Answers 12

up vote 55 down vote accepted

Install the package pdfgrep, then use the command:

find /path -iname '*.pdf' -exec pdfgrep pattern {} +
share|improve this answer
This works in mac osx (Mavericks) as well. Install it using brew. Simple. Thanks. – mikiemorales Jan 23 '14 at 1:28
Out of curiosity I checked the source of pdfgrep and it uses poppler to extract strings from the pdf. Almost exactly as @wag's answer only pagewise rather than, presumably, the entire document. – Andrew Martin Sep 16 '14 at 11:11
pdfgrep also has a recursive flag. So this answer could perhaps be reduced to: pdfgrep -R pattern /path/. Though it might be less effective if it goes through every file even if it isn't a PDF. And I notice that it has issues with international characters such as å, ä and ö. – Rovanion Jan 14 at 12:11

If you have poppler-utils installed (default on Ubuntu Desktop), you could "convert" it on the fly and pipe it to grep:

pdftotext my.pdf - | grep 'pattern'

This won't create a .txt file.

share|improve this answer
so .. you extract the text before you grep it which means the answer is "no". – akira Jan 31 '11 at 15:18
@akira The OP probably meant "without opening the PDF in a viewer and exporting to text" – Michael Mrozek Jan 31 '11 at 17:36
@Michael Mrozek: then that should be clarified by OP, but "grep only" is pretty clear to me. – akira Jan 31 '11 at 18:38
@akira Where do you see "grep only"? – Michael Mrozek Jan 31 '11 at 18:55
@akira Well, I already said what I think he probably meant; he doesn't want to export to text before processing it. I very much doubt he has a problem with any command that converts to text in any way; there's no reason not to – Michael Mrozek Feb 1 '11 at 5:52


A pdf consists of chunks of data, some of them text, some of them pictures and some of them really magical fancy XYZ (eg. .u3d files). Those chunks are most of the times compressed (eg. flat, check http://www.verypdf.com/pdfinfoeditor/compression.htm). In order to 'grep' a .pdf you have to reverse the compression aka extract the text.

You can do that either per file with tools such as pdf2text and grep the result, or you run an 'indexer' (look at xapian.org or lucene) which builds an searchable index out of your .pdf files and then you can use the search engine tools of that indexer to get the content of the pdf.

But no, you can not grep pdf files and hope for reliable answers without extracting the text first.

share|improve this answer

Recoll can search PDFs. It doesn't support regular expressions, but it has lots of other search options, so it might fit your needs.

share|improve this answer

You could pipe it through strings first:-

cat file.pdf | strings | grep <...etc...>
share|improve this answer
Just use strings file.pdf | grep <...>, you don't need cat – phunehehe Jan 31 '11 at 14:31
Yeah - my mind seems to work better with streams... :-) – Andy Smith Jan 31 '11 at 14:57
wont work if text is compressed, which it is most of the times. – akira Jan 31 '11 at 15:18
Even if the text is uncompressed, it's generally small pieces of sentences (not even necessarily whole words!) finely intermixed with formatting information. Not very friendly for strings or grep. – Jander Jan 31 '11 at 16:08
Can you think of another reason why using strings for this wouldn't work? I found that using strings works on some PDFs but not others. – hourback Nov 24 '15 at 19:58

Take a look at the common resource grep tool crgrep which supports searching within PDF files.

It also allows searching 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.

share|improve this answer

pdfgrep was written for exactly this purpose and is available in Ubuntu.

It tries to be mostly compatible to grep and thus provides "the power of grep", only specialized for PDFs. That includes common grep options, such as --recursive, --ignore-case or --color.

In contrast to pdftotext | grep, pdfgrep can output the page number of a match in a performant way and is generally faster when it doesn't have to search the whole document (e.g. --max-count or --quiet).

The basic usage is:

pdfgrep PATTERN FILE..

where PATTERN is your search string and FILE a list of filenames (or wildcards in a shell).

See the manpage for more infos.

share|improve this answer

try this

find /path -iname *.pdf -print0 | for i in `xargs 0`; do echo $i; \
    pdftotext "$i" - | grep pattern; done

for printing the lines the pattern occurs inside the pdf

share|improve this answer

There is a duplicate question on StackOverflow. The people there suggest a variation of harish.venkarts answer:

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

The advantage over the similar answer here is the --with-filename flag for grep. This is somewhat superior to pdfgrep as well, because the standard grep has more features.


share|improve this answer
I think it would have been better to leave this as a comment (or edit) in the similar answer you are referring to. – Bernhard May 9 '14 at 12:07

cd to your folder containing your pdf-file and then..

pdfgrep 'pattern' your.pdf

or if you want to search in more than just one pdf-file (e.g. in all pdf-files in your folder)

pdfgrep 'pattern'  `ls *.pdf`


pdfgrep 'pattern' $(ls *.pdf)
share|improve this answer

gpdf might be what you need if you're using Gnome! Check this in case you're not using Gnome. It's got a list of CLI pdf viewers. Then you can use grep to find some pattern.

Hope that helps.

share|improve this answer

Here is a quick script for search pdf in the current directory :


if [ $# -ne 1 ]; then
  echo "usage $0 VALUE" 1>&2
  exit 1


find . -name '*.pdf' -exec /bin/bash -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "$0"' $1 \;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.