Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to rotate all the images in a directory that match a pattern.

So far I have:

for file in `ls /tmp/p/DSC*.JPG`; do
  convert $file -rotate 90 file+'_rotated'.JPG

but that gives no output?

share|improve this question
up vote 10 down vote accepted

There are quite a few issues with your code. First of all, you are parsing ls which is a Bad Idea. You also need to refer to the variable as $file as you point out and you should also quote it so it won't break on spaces. You are declaring num but it is never used. A safer way would be:

find /tmp/p/ -name "DSC*.JPG" | while read file; do
  convert "$file" -rotate 90 "$file"_rotated.JPG

This will still have problems if your files contain new lines or other weird characters but at least will not break if your path contains spaces.

If the files are all in the same directory, it can be further simplified using globbing. You can also use parameter expansion to create foo_rotated.JPG1 instead of foo.JPG_rotated.JPG:

for file in /tmp/p/DSC*.JPG; do
  convert "$file" -rotate 90 "${file%.JPG}"_rotated.JPG
share|improve this answer
Your method will create original_filename.JPG_rotated.JPG". adding "${file%.JPG}"_rotated.JPG will be more appropriate imho. Cheers! – val0x00ff Sep 29 '13 at 17:39
@val0x00ff absolutely (and I upvoted your answer which suggests that). I just repeated the exact thing the OP was doing fixing the more glaring errors. – terdon Sep 29 '13 at 18:11
cool! I just modified your command a little. – val0x00ff Sep 29 '13 at 18:19

Do not parse ls and the ls is not required here. Furthermore, you should quote your variables in case they contain spaces.

for file in *.JPG; do
  convert -rotate 90 "$file" rotated_"$file"
share|improve this answer
space... or tabs, or newline or asterisk or question mark or square bracket... – Stéphane Chazelas Sep 29 '13 at 19:41

A simple method using BASH's PE (Parameter Expansion) is

for f in /tmp/p/DSC*.JPG
  convert -rotate 90 "$f" "${f%.JPG}"_converted.JPG
share|improve this answer
It should be noted that there's nothing bash specific in there, it's perfectly standard POSIX sh syntax. – Stéphane Chazelas Sep 29 '13 at 19:39

Not an imagemagic solution, but

sips -r 90 *.JPG

will rotate all images ending in .JPG 90 degrees. It's a good one liner.

share|improve this answer
fwiw, this overwrites the original images. – don_crissti Sep 8 '15 at 16:46

I needed to refer to the file as $file, i.e.

for file in `ls /tmp/p/DSC*.JPG`; do
  convert $file -rotate 90 $file+'_rotated'.JPG
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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