I work with lots of rather large imaged documents ranging from a few bites to multiple gigs of data per file. I need these documents to take up less storage space and especially to be more electronically portable and transmittable.

For single documents, I've been using:

ps2pdf -dPDFSETTINGS=/ebook inputfile.pdf outputfile.pdf

Which code I found here on AskUbuntu, and it works great with individual files yielding around a 1 to 0.42 compression ratio resulting in an output file of less than half of the original size while maintaining an acceptable level of reading quality.

I want to now use this code for my pdf library of documents to convert ALL my previous pdf files using a single command from the terminal. My strategic objective is to avoid having to compress each individual pdf file individually; I want to type a line of code, hit enter, and then go do something else productive and come back and find this done, and I'm hoping to mogrify the files in place (as opposed to converting them, which would produce doubles of each file) in order to avoid having to delete the old files.

All these files are together in a single folder.

Any suggestions?

Thank you in advance!

  • If the PDF has been properly created, it is already well compressed, and applying additional compression will not have much effect. Depending on the documents, there are some possibilities to make them smaller, but all of them would be lossy; if that's what you accept, it may work.
    – Max Wyss
    Feb 26, 2015 at 9:44
  • I'm well aware of the quality issue involved here. For those with answers, these files are primarily black & white reading documents; as long as the words can be read, the quality can be a little grainy. Portability and transmitability are most important for this project. Presently I have been targeting 25% compression (as in the output file is only 25% the bytes of the original input file yielding ~75% reduction in file size. Does anybody out there have a command line I can use to automate this task in order to convert every PDF in a given folder/directory. Thanks again! Feb 27, 2015 at 23:21
  • So, the documents are essentially text. That does, however, mean that any direction with images is suboptimal. Did you run tests with OCR (maybe using ClearScan in Acrobat)? That should give considerably smaller documents.
    – Max Wyss
    Feb 28, 2015 at 11:14

3 Answers 3


You can use the same command or mogrify as you've said, just by adding a bit of bash.

find $HOME/PDF_Lib -iname '*.pdf' | while read pdf; do ps2pdf -dPDFSETTINGS=/ebook "$pdf" "${pdf:0:(-4)}_new.pdf"; done


find $HOME/PDF_Lib -iname '*.pdf' | while read pdf; do mogrify -resample 150 -compress JPEG -quality 80 "$pdf"; done


create sh file in PDF directory and add this lines:


for f in *.pdf
    echo $f;
    convert -compress Zip $f  a$f;

now open your terminal in PDF FILES directory:

$ chmod +x script.sh
$ ./script.sh

now this script will compress every PDF file in your directory


I used the following code with success on iOS terminal to compress multiple PDFs recursively. I posted it because I couldn't find something that worked for me with a simple copy and paste.

find . -name '*.pdf' | while read pdf; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${pdf}_new.pdf" "$pdf"; done

Note you may want a different output quality, sou you can change the -dPDFSETTINGS parameter as follows:

-dPDFSETTINGS=/screen: lower quality, smaller size.
-dPDFSETTINGS=/ebook: for better quality, but slightly larger pdfs.
-dPDFSETTINGS=/prepress: output similar to Acrobat Distiller "Prepress Optimized" setting.
-dPDFSETTINGS=/printer: selects output similar to the Acrobat Distiller "Print Optimized" setting.
-dPDFSETTINGS=/default: selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

  • 1
    Welcome to AskUbuntu, many of readers expect you to explain a little of your code. E.g. how do you change the compression level or the hint for maximum ratio of the compressed pdfs are not readable any more. Apr 11, 2020 at 2:29
  • Thanks for the advice, @SadaharuWakisaka.
    – Zabala
    Apr 12, 2020 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.