I have been using this to get a page count of PDFs in shell scripts in OS X:

pdftk "$i" dump_data | grep NumberOfPages | awk '{print $2}' | tr -dc '[0-9]'

(where $i is the PDF filename)

However, there are times when that does not seem to work, so I would like to find another way to do it.

6 Answers 6


Yet another exiftool solution:

cd /path/to/pdf/directory
exiftool -T -filename -PageCount -s3 -ext pdf .
  • This one is very good IMHO. It (1) is simple (2) produces a clean output: file name [spaces] page count (3) can process all pdf files in a dir even if there are other files (4) will probably work on computer with different locales (5) is portable to other systems (using it on Linux). Apr 12, 2017 at 5:36
  • Great solution! Using this right now to count pages in thousands of files recursively and pipe the output to a file. Command is then like this: exiftool -T -r -filename -PageCount -s3 -ext pdf . > pages.txt
    – flomei
    May 22, 2018 at 11:07

You should be able to use mdls to view the metadata attributes for a PDF:

mdls -n kMDItemNumberOfPages "$i" | cut -c24-

  • Note: This solution only works if the target file resides on a volume/location that's being indexed. Personally, I have some locations excluded so for me this would not work in some cases. That said, it's a better way then having to pipe it so many times like shown in the OP. It's worth a +1 though! :) Aug 4, 2015 at 22:55
  • 1
    Even easier: mdls -raw -name kMDItemNumberOfPages I didn't realize there was an attribute for this. All of my files are on indexed HFS+ devices, so that will work fine.
    – TJ Luoma
    Aug 5, 2015 at 1:41

You can use qpdf command line utility to count the number of pages in a PDF document. qpdf can be installed via Homebrew via running the following command:

brew install qpdf

Once installed, to count the number of pages execute:

qpdf --show-npages file.pdf


Whether the file is indexed or not this works however if the target file does not resides on a volume/location that's being indexed then here is an alternative to the excellent solution presented by jordanmerrick.

I just tried this using exiftool and if it's just the page count you want as a number then this works:

exiftool "$i" | awk -F": " '/Page Count/{print $2}'

mdls did not work for me either. I did this on a Mac, but I expect it'll be the same on any bash or similar shell. With the Mac I have homebrew already installed so I just typed the command "homebrew install exiftool" first.

Here is the script I ran, you can set it up for your purposes. I had a whole folder of PDF files and I wanted page counts on every one of them so I had it look through the files and print out the filename and page with a , in between


for f in $FILES
    fn=$(basename "$f")
    printf "$fn"
    pg=$(exiftool "$f" | grep 'Page Count' | cut -c35-)
    printf ",$pg\n"

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 

gs \
  -o $output \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/screen \
  -dCompatibilityLevel=1.8 \
  -dDetectDuplicateImages=true \
  -dEmbedAllFonts=false \
  -dSubsetFonts=true \
  -dConvertCMYKImagesToRGB=true \
  -dCompressFonts=true \
  -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]

You must log in to answer this question.

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