↑ ↓ select, Enter open, Esc close

Precompress CSS, JS and SVG files with gzip and Brotli

Caution: This command changes the system. Understand what it does before you run it.

Adjust the values, the command updates live
user@server
find /var/www/vhosts/example.com/httpdocs/wp-content/themes -type f \( -name '*.css' -o -name '*.js' -o -name '*.svg' \) -exec gzip -k -9 -f {} + -exec brotli -k -f {} +

Creates a datei.css.gz and a datei.css.br next to the original at the highest compression level, keeping the original (-k). With gzip_static on, nginx serves such files directly without compressing again on every request. Existing compressed versions are overwritten (-f).

Note: Install the brotli package if needed. For .br, nginx needs the Brotli module with brotli_static. Run it again after every change to the originals, otherwise the server delivers outdated versions.

Also searched as

  • create files for nginx gzip_static
  • precompress assets with brotli
  • prepare static compression
  • generate css.gz and js.br files

Measure gzip and Brotli savings for a URL

$for e in identity gzip br; do curl -s -o /dev/null -H "Accept-Encoding: $e" -w "$e: %{size_download} Bytes\n" https://example.com/; done
harmlessImages and performance

Check whether a website serves gzip or Brotli

$curl -s -o /dev/null -D - -H "Accept-Encoding: br, gzip" https://example.com/ | grep -i "^content-encoding"
harmlessnginx

Convert images to AVIF recursively

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec sh -c 'for f; do [ -e "$f.avif" ] || avifenc -s 6 "$f" "$f.avif" >/dev/null; done' sh {} +
cautionImages and performance

Convert images to WebP recursively

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec sh -c 'for f; do [ -e "$f.webp" ] || cwebp -quiet -q 80 "$f" -o "$f.webp"; done' sh {} +
cautionImages and performance

Find large images in wp-content/uploads

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.webp' \) -size +500k -printf '%s\t%p\n' | sort -rn | head -50 | numfmt --field=1 --to=iec
harmlessImages and performance

Find unminified CSS and JS files

$find /var/www/vhosts/example.com/httpdocs/wp-content -type f \( -name '*.css' -o -name '*.js' \) ! -name '*.min.*' -size +20k -exec sh -c 'echo "$(wc -l < "$1") $1"' sh {} \; | awk -v n=200 '$1 > n' | sort -rn | head -30
harmlessImages and performance

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.