Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Perhaps running pdfinfo (here on Fedora in the poppler-utils package) gives a clue?

Most of the information on a PDF file is in the dictionary at end, so if it finds it it should be OK. I'd do something like:

for f in *.pdf; do
  if pdfinfo "$f" > /dev/null; then
    : Nothing
  else
    echo "$f" is broken
  fi
done
share|improve this answer
    
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 at 19:29

I got myself an answer:

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

PDFs with errors will show errors.

share|improve this answer
3  
It is a bad idea (and never really needed) to iterate over the output of ls: mywiki.wooledge.org/ParsingLs –  slhck Apr 10 '13 at 14:06
2  
@slhck: This should be handled with find (1). :-) –  Martin Schröder Apr 11 '13 at 13:15

This is my script

find . -iname '*.pdf' | while read -r f
  do
    if pdftotext "$f" &> /dev/null; then 
        mv "$f" "$f.pdf";
        echo "$f" was ok;   
    else
        mv "$f" "$f.broken";
        echo "$f" is broken;   
    fi; 
done
share|improve this answer

Your Answer

 
discard

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.