I have a large set of JPEG pictures all with the same resolution. It would take too long to open each one inside the graphical interface of imagemagic or gimp.

How do I achieve each picture being rotated and saved as the same filename?


4 Answers 4


You can use the convert command:

convert input.jpg -rotate <angle in degrees> out.jpg

To rotate 90 degrees clockwise:

convert input.jpg -rotate 90 out.jpg

To save the file with the same name:

convert file.jpg -rotate 90 file.jpg

To rotate all files:

for photo in *.jpg ; do convert $photo -rotate 90 $photo ; done

Alternatively, you can also use the mogrify command line tools (the best tool) recommended by @don-crissti:

mogrify -rotate 90 *.jpg
  • 1
    thx, how do I tell the command that the filename left of -rotate -180 has to be the same as right of it , if there are many files?
    – sharkant
    May 17, 2017 at 11:42
  • Rotating a file 180 degrees will always give you the same result, no matter if you rotate it left or right. May 17, 2017 at 14:17
  • @GAD3R 360 degrees will result in the same image afterwards. Rotating an image left or right by 180 degrees, with both give you the same image (but upside down). May 17, 2017 at 14:21
  • 2
    $CAPITAL_NAMES should be left to environment variables; for those local variables in shell scripts or commands use $normal_lowercase
    – cat
    May 17, 2017 at 16:46
  • 4
    convert re-encodes JPEG and hence is lossy. Better use jpegtran Aug 6, 2017 at 12:44

For JPEG images and right-angle rotations, use jpegtran or exiftran, as they can rotate the images losslessly.

for f in *.jpg ; do 
    jpegtran -rotate 180 "$f" > "${f%.jpg}-rotated.jpg"

Or to rotate in-place:

for f in *.jpg ; do
    jpegtran -rotate 180 -outfile "$f" "$f"

exiftran also has the -a flag to automatically rotate the image based on what the EXIF orientation tag says.

  • For counter-clockwise (left-angle) rotation with jpegtran, use -rotate 270 and with exiftran use -2flag according to the manual.
    – Timo
    Mar 12, 2018 at 19:47
  • *.jpg works under bash but under zshell it causes the error jpegtran: can't open *.jpg for reading but *jpg works for me in zshell. Dec 28, 2020 at 10:09

Note that the two other answers may provide different results depending on the EXIF Orientation : it seems that convert rotates with regards to the EXIF Orientation, while jpegtran just ignores the EXIF Orientation.

This observation led me to figure I actually needed to discard the EXIF Orientation, so I just used exiftool to discard EXIF data without further data loss (that's also what does jpegtran when no -rotate option is given, it seems) :

exiftool -all= -o outfile.jpg infile.jpg

I could have just removed the EXIF Orientation with

exiftool -Orientation= -o outfile.jpg infile.jpg

or modified it with

exiftool -n -Orientation=1 -o outfile.jpg infile.jpg

(for this later case you will need to read the FAQ to understand option -n, needed for exiftool to translate the -Orientation value, and the EXIF tags table).


This is an older question, however I found it whilst searching for a solution for this very task.

The problem with jpegtran is that it does rotate the image, but it discards all the EXIF header data.

exiftool keeps it, but it's not very handy to use.

Thus, I wrote a small script to rotate a JPEG image by 90°, keeping the EXIF header. Maybe someone may find this useful:


orientation=$(exiftool -n -Orientation "$1")

if [[ "$?" != "0" ]]; then
    exit 1

orientation=${orientation##*: }

if [[ "$orientation" == "1" ]]; then
    # 1 = Horizontal (normal)
elif [[ "$orientation" == "6" ]]; then
    # 6 = Rotate 90 CW
elif [[ "$orientation" == "3" ]]; then
    # 3 = Rotate 180
elif [[ "$orientation" == "8" ]]; then
    # 8 = Rotate 270 CW
    echo "Can't process orientation \"$orientation\""
    exit 1


mv "$1" "$backup" || exit 1

exiftool -n -Orientation=$target "$backup" -o "$1"
  • 2
    jpegtran has -copy all to copy exif and other metadata. Note that it doesn't rotate the thumbnail if any. May 25 at 19:55
  • Beware jpegtran will corrupt images that don't have width and height being precisely a multiple of 8 pixels (a sliver of the picture from one edge gets moved to the opposite edge). This behaviour is alluded to in the man page ("The transpose transformation has no restrictions regarding image dimensions. The other transformations operate rather oddly if the image dimensions are not a multiple of the iMCU size (usually 8 or 16 pixels), because they can only transform complete blocks of DCT coefficient data in the desired way"). Sep 9 at 7:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .