Below are some simple commands that work for ImageMagick 7 and later. I haven’t tested them to work on the older ImageMagick 6 which surprisingly is still being supported and the default installed version for many Linux distros.

Batch Compressing Images

# Make new compressed directory
mkdir compressed

# Example of compressed exiting .jpg images
magick mogrify -path compressed -quality 85 -strip -define jpeg:preserve-settings *.jpg

# Preserve the original file timestamps
for file in *.jpg; do
  touch -r "$file" "compressed/$file"
done



Batch Convert

# Make new directory for converted files
mkdir avif

# Convert the .jpg files to .avif
magick mogrify -path avif -format avif -quality 85 *.jpg

# Copy time/date stamp from original .jpg file to newly converted .avif files
for file in *.jpg; do
  touch -r "$file" "avif/${file%.jpg}.avif"
done



Optimizing PNG Images

# Simple Mod
magick input.png -optimize -strip output.png

# Replaces optimized file over input/original file
magick input.png -optimize -strip -set modify_date "$T" output png -mv out.png input.png