Alejandro Muñoz Fernández Page

Ultimate guide to compressing files as much as possible in Linux

Over time I’ve picked up different ways to compress files in Linux — from stubborn PDFs to squeezing a few extra KB out of images. There’s almost always a bit more room for optimization.

The goal isn’t just to compress, but to do it properly: keeping compatibility and finding the best balance between size and quality.

While there are GUI tools, the terminal gives you much more control and often better results. This is a collection of the ones that have worked best for me, with practical examples. No need to memorize complex commands — just copy and paste.


1. Documents

1.1. PDF (.pdf)

Probably one of the most common formats and also one of the most problematic when file sizes get out of hand.

First, install ghostscript:

sudo apt install ghostscript

1.1.1. Single file compression

This command reduces the size quite a bit (you can copy the whole block; the backslashes \ indicate line continuation):

gs -q -dNOPAUSE -dBATCH -dSAFER -dQUIET -dDetectDuplicateImages \
-dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true \
-sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen \
-dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic -dColorImageResolution=128 \
-dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=128 \
-dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=128 \
-sOutputFile=compressed.pdf original.pdf

Then you can try squeezing it a bit more with pdftk:

sudo apt install pdftk
pdftk compressed.pdf output final_compressed.pdf compress

⚠️ Note: This step may remove bookmarks or the table of contents in some PDFs. Always check the result.

1.1.2. Batch compression

To process all PDFs in a directory:

find . -name '*.pdf' | while read pdf; do
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen \
    -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages \
    -dCompressFonts=true -r150 \
    -sOutputFile="${pdf%.pdf}_compressed.pdf" "$pdf"
done

2. Images

2.1. GIF (.gif)

GIFs can usually be reduced quite a bit with minimal visible quality loss:

sudo apt install gifsicle
gifsicle -b -n -O3 image.gif

2.2. PNG (.png)

For PNGs, combining tools often gives the best results.

First optipng:

sudo apt install optipng
optipng -o7 -zm1-9 -clobber -fix -quiet -strip all image.png

Then zopflipng:

sudo apt install zopfli
zopflipng -m -y image.png image_opt.png

2.3. JPEG (.jpg / .jpeg)

For photos, jpegoptim is fast and effective:

sudo apt install jpegoptim
jpegoptim --strip-all -m75 -v photo.jpg

Another option is mozjpeg (slower, sometimes better results):

sudo apt install libjpeg-turbo-progs

Basic usage:

cjpeg -quality 75 -outfile output.jpg input.jpg

Personally, I usually stick with jpegoptim for its speed and good overall balance.


3. Already compressed files

It may sound surprising, but some compressed files can still be optimized further.

3.1. ZIP (.zip)

Many modern formats (docx, epub, odt…) are actually ZIP files.

You can recompress them with advzip:

sudo apt install advancecomp
advzip -z -p -4 -k -i 1000 file.zip

3.2. Gzip (.gz)

zopfli creates gzip-compatible files that are smaller:

sudo apt install zopfli

Process:

gunzip file.gz
rm file.gz
zopfli -i100 file

4. Web assets

Here we’re not really “compressing” but minifying.

4.1. XML (.xml, .svg, etc.)

htmlcompressor is a very handy tool:

wget -c https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/htmlcompressor/htmlcompressor-1.5.3.jar
sudo apt install openjdk-11-jre-headless

Usage:

java -jar htmlcompressor-1.5.3.jar -t original.xml -o compressed.xml

It removes whitespace, line breaks, and unnecessary comments.


4.2. HTML (.html)

java -jar htmlcompressor-1.5.3.jar -t html -c utf-8 \
--simple-doctype --remove-style-attr --remove-link-attr \
--remove-script-attr --simple-bool-attr \
--remove-js-protocol --remove-surrounding-spaces max \
original.html -o compressed.html

4.3. JavaScript (.js)

yui-compressor is a classic and still reliable:

sudo apt install yui-compressor
yui-compressor --compilation_level ADVANCED_OPTIMIZATIONS \
--js original.js \
--js_output_file compressed.js

5. Conclusion

There’s almost always a bit more that can be optimized. With the right terminal tools, you can reduce the size of almost any file quickly, reproducibly, and often without noticeable quality loss.

If you know any tricks or tools that improve these results, feel free to share them.

Tags: