I have a directory with a lots of images but they are in the wrong orientation. I want to rotate the images to correct the orientation (mostly ±90o). Using image (shotwell photo) viewer I can rotate them individually by clicking the rotate button but that's too tedious.

I looked at man shotwell and shotwell --help-all but there's nothing that explains how to invoke the rotate command from the command line.

Is there any way I can invoke the rotate command of shotwell (or any other viewer) from the terminal? Or any other methods to rotate images are welcome too.


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

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

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)

  • This isn't lossless. – Geremia Jun 27 '20 at 21:24
  • @Geremia That depends on the file format used. If it's a TIFF or BMP that you rotate and (keep the same destination file format) it will be perfectly lossless. If you use JPEG or PNG (both lossy file formats) the result will be lossy as well, but that was not what the original question was about... 0:-) – Fabby Jul 5 '20 at 17:03
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
  • Works perfectly – Paul Jones Jun 12 '19 at 11:44
  • Mogrify is quite a Swiss knife when it comes to simple, quick, batch image editing – EA304GT Jul 23 '19 at 18:49
  • best answer, why wouldn't you want to do this in-place? – mLstudent33 Nov 9 '19 at 19:23
  • 1
    @mLstudent33: Good question. It appears that mogrify isn't lossless for JPEG files, but jpegtran is. – krlmlr Nov 10 '19 at 20:11

Here's how I do it:

  1. Install gThumb

     sudo apt-get install gthumb
  2. Open up nautilus and go to your images directory. Right click on one and choose Open with -> gthumb.

  3. Under the view menu choose Browser or press the Esc key. This will open the directory browser showing all your images.

    enter image description here

  4. Press Ctrl and select the images you want to rotate or select all of them via Ctrl + A.

  5. On the toolbar, select Tools and then Rotate Right or Rotate Left depending on your preference.

    enter image description here


A nice solution is to make a set of backups (e.g. prefixed backup-), rotate the original files producing a set of new files (prefixed e.g. rotated-), giving you a set of

  • img-1.png
  • backup-img-1.png
  • rotated-img-1.png
  • img-2.png
  • ...and so on

The mv/cp tools [bash globbing] can only add prefixes, it's messy to take them away (it'd use parameter expansion, ewww...)

The rename tool allows you to use s/before/after/ substitution syntax (from the sed tool) to swap that safeguard prefix and overwrite the original files, so your overall process for a given set of pictures img-{1..n}.png would be:

for imgf in img-*.png; do mv "$imgf" "backup-$imgf"; done
for imgf in backup-img-*.png; do convert "$imgf" -rotate 90 "rotated-$imgf"; done


  • you could use cp rather than mv, but then there's 2 copies of the original lying around (downside is you get concatenation of prefixes, "rotated-backup-...")
  • rotation is clockwise (-rotate 270 gets you 90° anti-clockwise)
  • to track progress, add echo "Rotating ${imgf#backup-} ... "; after convert calls (before done)
  • for a more compact form (e.g. a set of numbered files), use some parameter expansion like echo "$(echo ${imgf#backup-img-} | cut -d\. -f 1)..."; instead

    ( You can't remove prefix and suffix in the same bash param expansion hence use cut )

Then after verification you've not messed up, delete the pictures by moving them back to the original

rename 's/^rotated-//;' rotated-*
rm backup-img-*
  • thx for your answer. would you know if this is lossy for jpegs? – BKSpurgeon Oct 16 '20 at 10:41

You can copy/paste this code, and save it as rotate.sh

#!/bin/bash -e

cd $1
for file in *.jpg
    convert $file -rotate 90 $file

After saving this file, run it from terminal using ./rotate.sh folder_containing_images.

  • Please check your code and the link given! – George Udosen Mar 31 '17 at 23:28
  • This runs serious risk of data loss! You must use quotation marks, thus: CUR_DIR="$PWD" (note the simplification of using the built-in variable), cd "$1", convert "$file" -rotate 90 "$file" and cd "$CUR_DIR" – Paddy Landau Nov 11 '20 at 13:04
  • You can simplify further by using pushd and popd. They're great for changing directory and back again. – Paddy Landau Nov 11 '20 at 13:05

How about exiftran - transform digital camera jpeg images

I know it's a bit limiting (jpeg) but it has automatic (using exif orientation tag).

for photo in *.jpg
  do exiftrans -ia $photo

About Exiftran, it is a command line utility to transform digital camera jpeg images. It can do lossless rotations like jpegtran(1), but unlike jpegtran(1) it cares about the EXIF data: It can rotate images automatically by checking the exif orientation tag; it updates the exif informations if needed (image dimension, orientation); it also rotates the exif thumbnail. It can process multiple images at once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.