How do I search a PDF file from command line?

Just to add to the above answer, in particular you can use a command line tool from xpdf-utils called pdftotext and then search the text document created by this tool with grep.

This might look something like this:

pdftotext document.pdf - | grep -C5 -n -i "search term"

There is more information in the manual. The only drawback to pdftotext is that you can't us globbing to transform multiple files at the same time. This problem can be overcome with a small bash script:

for f in pdf_directory; do echo $f; pdftotext $f - | grep -i "search_term"; done

If you are having problems creating a text document from a pdf due to, for example, an incompatible pdf file, then that is another problem.

I think in general, pdf editors don't include command lines because they are graphical. If you want to use bash, (or maybe zsh!) then you might have to use a terminal shell.

Good luck!


poppler-utils

Note: xpdf-utils is a transitional package for poppler-utils.

You can use poppler-utils. poppler-utils is a suite of tools for Portable Document Format (PDF) files.

To install it you can use the Ubuntu Software Center, or by clicking below:

Install poppler-utils

pdfgrep

pdfgrep can search a string or a pattern in PDF files recursively in directory trees, counting matches or printing some context for each match. For example, to recursively search keyword in /some directory, case insensitive:

pdfgrep -Ri keyword /some/directory

Pdfgrep is a tool to search text in PDF files. It works similar to `grep'.

Features:

  • search for regular expressions.
  • support for some important grep options, including: + filename output. + page number output. + optional case insensitivity. + count
    occurrences.
  • and the most important feature: color output!

Install pdfgrep

1Source:Ubuntu Apps Directory


To search for a regular expression in multiple pdf files using pdfgrep:

find /path -iname '*.pdf' -exec pdfgrep -H 'pattern' {} \;

where path is location for your pdf files.