Rotate images from terminal

If you're looking for a pure bash implementation, ImageMagick's convert command is what you're looking for:

for szFile in /path/*.png
do 
    convert "$szFile" -rotate 90 /tmp/"$(basename "$szFile")" ; 
done

Above will leave existing files intact and copy the newly rotated ones to /tmp so you can move or copy them somewhere else or even replace the existing ones after the conversion and after verification.

(and it'll work on all recent releases of Ubuntu as it's standard software)


for file in *.JPG; do convert $file -rotate 90 rotated-$file; done

This will copy-rotate-and-rename your files.

If you want to leave the original files untouched just yet, this method may work well for you...

Note that this is case-sensitive: if your files are named *.jpg replace with lower-case (or *.png ...) accordingly.


If you want to overwrite in-place, mogrify from the ImageMagick suite seems to be the easiest way to achieve this:

# counterclockwise:
mogrify -rotate -90 *.jpg

# clockwise:
mogrify -rotate 90 *.jpg

CAVEAT: This isn't a lossless rotation method for JPEG files, https://www.imagemagick.org/discourse-server/viewtopic.php?t=5899. jpegtran achieves this (untested):

# counterclockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 270

# clockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 90