Modern PDF files produced by word processors, graphic design suites, and physical scanners frequently contain high-resolution uncompressed images, redundant metadata, and embedded font subsets that inflate file sizes into dozens or hundreds of megabytes. Sending such bulky documents via email, uploading them to job portals, or archiving them on low-capacity servers often requires shrinking their footprint without destroying text clarity or vector legibility.
In Linux, the open-source PostScript and PDF engine Ghostscript (invoked via the command gs) provides one of the most reliable and configurable command-line tools for compressing PDF files. This tutorial guides you through installing Ghostscript, understanding compression presets, and executing automated batch compression scripts in the Linux terminal.
Installing Ghostscript on Linux
Ghostscript is included in the official repositories of nearly all major Linux distributions. Verify whether it is installed by checking its version:
gs --version
If Ghostscript is not present on your system, install it using your package manager:
On Ubuntu, Debian, and Linux Mint:
sudo apt update
sudo apt install ghostscript
On Fedora, RHEL, and CentOS Stream:
sudo dnf install ghostscript
On Arch Linux and Manjaro:
sudo pacman -S ghostscript
Understanding Ghostscript PDF Compatibility and Presets
Ghostscript utilises a dedicated parameter named -dPDFSETTINGS to control the resolution of downsampled images and overall compression intensity. The primary presets include:
/screen: Downsamples images to 72 DPI. Produces the smallest possible file size, ideal for casual web reading and email attachments where high-resolution printing is unnecessary./ebook: Downsamples images to 150 DPI. Provides a balanced compromise between compact file size and sharp readability across desktop screens, tablets, and e-readers./printer: Downsamples images to 300 DPI. Preserves high quality suitable for domestic and office printing while stripping extraneous uncompressed overhead./prepress: Downsamples images to 300 DPI with colour preservation optimised for high-grade commercial press printing. Produces larger files with minimal loss./default: Applies balanced multi-purpose output suitable for general distribution.
How to Compress a PDF File Using Ghostscript
To compress an existing PDF document using the recommended /ebook preset, execute the following command syntax in your terminal:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
Here is what each flag controls:
-sDEVICE=pdfwrite: Directs Ghostscript to generate an optimised PDF document.-dCompatibilityLevel=1.4: Sets PDF specification compatibility to Acrobat 5.0 (PDF 1.4), ensuring maximum compatibility across all PDF viewers.-dPDFSETTINGS=/ebook: Configures the compression profile (substitute with/screenfor maximum size reduction).-dNOPAUSE: Disables prompting for user confirmation between pages during processing.-dQUIET: Suppresses verbose diagnostic messages and processing logs to keep terminal output clean.-dBATCH: Exits Ghostscript cleanly once processing completes.-sOutputFile=output.pdf: Specifies the path and filename for your newly compressed document.input.pdf: The target source file to compress.
Verifying File Size Reduction
You can check the original file size alongside your compressed file using the ls command with human-readable flags:
ls -lh input.pdf output.pdf
Often, scanned documents containing large unoptimised bitmap scans shrink by 70% to 90% without noticeable degradation of text legibility.
Batch Compressing Multiple PDFs in a Directory
If you have a collection of PDF documents that require compression, you can run a simple Bash loop directly in your terminal:
for f in *.pdf; do
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="compressed_${f}" "$f"
done
This script iterates through every PDF file in the current working directory and outputs an optimised copy prefixed with compressed_, safeguarding your original source documents against accidental loss.