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

The syntax for the command convert in ImageMagick is

convert source_filename.ext1  destination_filename.ext2

I would like to use zsh to feed it with files in a folder path_to_source and output the result (files with same name, but with a different extension) under the folder path_to_destination.

I believe I can use find and xargs for this, but I didn't come up with anything that worked. Also I presume that ZSH may have some builtins that are useful for this. Any pointers would be greatly appreciated.

NOTE: As far as I know, convert does not have a native way of handling a batch of files, so I need to feed the files one by one. In either case, I'm interested in solving the problem of feeding a command file by file with these arguments.

share|improve this question
up vote 5 down vote accepted
for x in /path/to/source/**/*.ext1; do convert $x ${x:r}.ext2 done

The r in ${x:r} is a history modifier. There's a short form of for that saves a few characters.

for x (/path/to/source/**/*.ext1) convert $x ${x:r}.ext2
share|improve this answer

I would do this with:

cd path_to_source
find . | sed 's/.ext1$//' | xargs convert {}.ext1 ../path_to_destination/{}.ext2
share|improve this answer

You can batch process your files with mogrify and convert from one format to another using the -path option (as of imagemagick v6.2.0) to specify a different directory in which to output the processed images:

mogrify -path /path/to/dest_filename -format ext2 /path/to/source_filename/*.ext1

so e.g.

mogrify -path /home/myjpgs -format jpg /media/images/*.png

will convert all .png images in the /media/images directory to jpg keeping the same filename (only the extension changes) and saving the resulting images to /home/myjpgs

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.