I have many PDF files on one folder.

Is it possible check if one or more files are corrupted (zero pages, or unfinished downloads) using the command line, without needing to open them one by one?

8 Answers 8


You can try doing it with pdfinfo (here on Fedora in the poppler-utils package). pdfinfo gets information about the PDF file from its dictionary, so if it finds it the file should be ok

for f in *.pdf; do
    if ! pdfinfo "$f" &> /dev/null; then
        echo "$f" is broken
  • 6
    I would suggest to replace pdfinfo with pdftotext. This way all text on every page will be checked. And the > gt character should be &> so that all the error messages don't show up.
    – schoetbi
    Oct 17, 2014 at 19:29
  • All my PDFs are flagged as broken. Hundreds of gigabytes of them. Including ones I just created. Whether using pdfinfo or pdftotext...
    – PatrickT
    Mar 11, 2016 at 7:20
  • What i've found is that pdfinfo reports various types of SyntaxError, and some are tolerated while others are not. For example, files with Syntax Error: Invalid object stream can be opened but files with Syntax Error: Couldn't read xref table cannot. Oct 4, 2022 at 13:01

My tool of choice for checking PDFs is qpdf. qpdf has a --check argument that does well to find problems in PDFs.

Check a single PDF with qpdf:

qpdf --check test_file.pdf

Check all PDFs in a directory with qpdf:

find ./directory_to_scan/ -type f -iname '*.pdf' \( -exec sh -c 'qpdf --check "{}" > /dev/null && echo "{}": OK' \; -o -exec echo "{}": FAILED \; \)

Command Explanation:

  • find ./directory_to_scan/ -type f -iname '*.pdf' Find all files with '.pdf' extension

  • -exec sh -c 'qpdf --check "{}" > /dev/null && echo "{}": OK' \; Execute qpdf for each file found and pipe all output to /dev/null. Also print filename followed by ': OK' if return status of qpdf is 0 (i.e. no errors)

  • -o -exec echo "{}": FAILED \; \) This gets executed if errors are found: Print filename followed by ": FAILED"

Where to get qpdf:

qpdf has both Linux and Windows binaries available at: https://github.com/qpdf/qpdf/releases. You could also use your package manager of choice to get it. For example on Ubuntu you can install qpdf using apt with the command:

apt install qpdf
  • 1
    However, qpdf --check does not detect multiply defined metadata, which are incorrect as they are handle differently by different tools. I've reported a bug. Other tools such as pdfinfo and pdftk do not either, but they do not claim to check the PDF structure.
    – vinc17
    Jul 30, 2019 at 13:50
  • @vinc17 Has this bug been fixed? Jul 13, 2020 at 13:27
  • @user1424739 Yes, this was fixed last year in September, and I could check. For instance, I now get: "WARNING: metadata.pdf (object 16 0, offset 9167): dictionary has duplicated key /Title; last occurrence overrides earlier ones".
    – vinc17
    Jul 14, 2020 at 0:50
  • qpdf --check has found issues neither pdfinfo nor pdftotext could find, e.g., "extraneous whitespace seen before xref" after changing the length of the /CreationDate field. See tex.stackexchange.com/q/570161.
    – bers
    Nov 19, 2020 at 20:43
  • qpdf gives false positives, way more than true positives.
    – ceremcem
    Mar 14, 2021 at 9:41
find . -iname '*.pdf' | while read -r f
    if pdftotext "$f" - &> /dev/null; then 
        echo "$f" was ok;   
        mv "$f" "$f.broken";
        echo "$f" is broken;   
  • 1
    To clarify: This script renames the pdf files that are diagnosed as 'broken' by appending .broken to the .pdf extension.
    – PatrickT
    Mar 11, 2016 at 7:07
  • +1 find is indeed a good tool for the job. to simply list the broken pdf files beneath current directory, this bash find command does it all using a redirection 3>&1 2>/dev/null find . -iname '*.pdf' ! -exec pdfinfo '{}' \; -fprint /dev/fd/3 &>/dev/null
    – Jack Wasey
    Jun 9, 2021 at 9:50

All of the methods using pdfinfo or pdftotext have not worked for me. In fact they kept giving me false positives and sometimes created files I didn't need.

What did work was JHOVE.


Install the jar from the above link and update your PATH environment variable with this command:

echo "export PATH=\$PATH:/REPLACE_WITH/YOUR/PATH_TO/jhove/" >> ~/.bash_profile

Refresh each terminal with source ~/.bash_profile and you're good to start using it system wide.

Basic Usage:

jhove -m pdf-hul someFile.pdf

You'll get a lot of info about the pdf - more than most people probably need.

Bash One-Liner:
Simply returns valid or invalid:

if [[ $(jhove -m pdf-hul someFile.pdf | grep -a "Status:") == *"Well-Formed and valid"* ]]; then echo "valid"; else echo "invalid"; fi;

Note that this was run on Mac OS X but I assume it works the same with any Unix based Bash environment.


I got myself an answer:

for x in *.pdf; do echo "$x"; pdfinfo "$x" | grep Pages; done

PDFs with errors will show errors.


There are different ways to do this. It depends on what exactly you want to check.

Different commands behave differently, and some exit with status 0 - even if there were some errors.

Also it depends on whether you treat a Warning (possibly also with exit status 0) as an indication of a corrupt file. And, finally, even if there are some errors/warnings, it depends on what that error/warning is actually about (maybe a corrupt embedded image is not a big problem for you, and you consider such PDF file as valid). There are many things to decide on, and trying different tools may be beneficial.

I have a database of 5031 PDF files, and I have tested them with the following commands:

  1. pdfinfo file.pdf (~3 min)
  2. pdftotext -layout file.pdf - (~29 min)
  3. qpdf --check file.pdf (~222 min)

for the presence of any kind of output to stderr, and saved that output to the spreadsheet: https://docs.google.com/spreadsheets/d/1UA9HOKW9rYnUOQ5JAnFUwZ7N6YftSotzhe46zBgiEJY/edit?usp=sharing

I filtered the rows by the presence of any output to stderr from ANY command for a file. Every cell contains the full stderr output - double click on it to see the content.

pdfimages -list file.pdf - gives exactly same errors as pdftottext

So you can test the files with all or selected testing commands the following way:

for file in *
    if stderr=$((\
        pdfinfo $file && \
        pdftotext -layout $file - && \
        qpdf --check $file) 2>&1 >/dev/null) && test -z "$stderr"
        echo 'file is ok'
        echo 'file is NOT OK'

This script checks both testing commands exit status and ANY non-empty output to stderr.

It doesn't print out the standard output from the testing commands.


In addition to the tools mentioned above, the pdfcpu library/tool also has PDF validation functionality:

pdfcpu validate whatever.pdf

Note pdfcpu is still in Alpha at the time of writing (August 2020).


In simple words, pdf is an especially structured form of PostScript. qpdf is probably a good tool to test the structure of the file, but PostScript is a programming language. Checking the syntax of the PostScript part is a good idea, but this is not sufficient. At the run time, many control structures are passed, many functions are called and not all the passed values are always valid. Only at the run time, you will see if all this is running well and if the result is what you want. Further, not all fonts are always included in a pdf file. Missing fonts, which are not available at run time, can cause many problems. The utility pdffonts can help you to analyze such problems which can occur here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .