Another solution is to use the pdfinfo
command from the xpdf package. Once installed, you can get the number of pages alongside the file name as follows:
pdfinfo /path/to/file.pdf | grep -- ^Pages
Or as a batch run (with the output format you asked for):
while read pdf; do
pages=$(pdfinfo "$pdf" 2>&1 | grep -- ^Pages | tr -dc '[0-9]')
printf "%s %d\n" "$pdf" "$pages"
done < <(find /path/to/pdfs -name "*.pdf" -noleaf -type f)
pdfinfo
in general outputs the following information:
Creator: Microsoft� Office Word 2007
Producer: Microsoft� Office Word 2007
CreationDate: Tue Feb 25 14:46:56 2014
ModDate: Tue Feb 25 14:46:56 2014
Tagged: yes
Form: none
Pages: 3
Encrypted: no
Page size: 595.32 x 841.92 pts (A4) (rotated 0 degrees)
File size: 102155 bytes
Optimized: no
PDF version: 1.5
I use it frequently to check if the pdf is "optimized" or not. In case it's not optimized, I can then run it through the following script to reduce its file size (often significantly):
#!/usr/bin/env bash
#
# makeSmall.sh
input=$1
output=$2
gs \
-o $output \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/screen \
-dCompatibilityLevel=1.8 \
-dDetectDuplicateImages=true \
-dEmbedAllFonts=false \
-dSubsetFonts=true \
-dConvertCMYKImagesToRGB=true \
-dCompressFonts=true \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-c '<</AlwaysEmbed [ ]>> setdistillerparams' \
-c '<</NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats /Arial]>> setdistillerparams' \
-f $input
$ pdfinfo F.pdf | grep -- ^Optimized
Optimized: no
$ ./makeSmall.sh F.pdf F-reduced.pdf 2>/dev/null
$ gstat --printf "%13n [size=%6s Bytes]\n" F*.pdf
F-reduced.pdf [size= 15983 Bytes]
F.pdf [size=102155 Bytes]