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
Note:
- 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-*