How to compress entire folder of PDFs using ghostscript (gs) on the command line

This bash script will compress all PDFs (*.pdf) in the current directory. For an input file named a.pdf it will write the output file to a.compressed.pdf. The input file won’t be changed.

export IMAGE_DPI=72
for i in *.pdf ; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -dSAFER -dDownsampleColorImages=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=$IMAGE_DPI -dGrayImageResolution=$IMAGE_DPI -dMonoImageDownsampleType=/Subsample -dMonoImageResolution=$IMAGE_DPI -dEmbedAllFonts=true -dSubsetFonts=true -sOutputFile="$i.compressed.pdf" "$i" ; done

This command will mostly reduce the PDF size by downsampling the image resolution. It is preset to a resolution of 72 dpi (export IMAGE_DPI=72).

72 dpi is a pretty low resolution, but this will significantly decrease the resulting size of the compressed PDF. If you prefer good image quality to small filesize, you should choose a larger value.