I need to convert a bunch of .rgb files into .pngs (each file should retain its original name).

I want to find out how to convert the first file and then wrap the command in a 'for' loop.

This is what i have so far:

find . -name "*.rgb" | sort | sed -n \1\p | xargs convert zzzzz.png

But this doens't work.

How can I make the convert command to recognize the second argument zzzzz.png?


If you want to convert all files in the dir-tree I would run:

find . -name '*.rgb' | parallel convert {} {.}.png

This will in parallel run:

convert ./foo.rgb ./foo.png
convert ./bar/quux.rgb ./bar/quux.png

Here I use GNU Parallel to make use of all your CPU's. Depending on your system you can install it as a package, otherwise it is here gnu.org/software/parallel. In Debian it is:

apt-get install parallel

Here is a solution without GNU Parallel:

find . -name '*.rgb' -exec sh -c 'convert "$0" "${0%.*}.png"' {} \;

and a not so nice solution using while..read:

find . -name '*.rgb' | while read FILE; do convert "${FILE}" "${FILE%rgb}png" ; done
  • I couldn't get the parallel version to work under OSX - it just did nothing (a problem with parallel itself I think). Thanks for posting a working find..exec alternative, it worked flawlessly. – antonyh Apr 21 '17 at 20:48

I found the answer:

for i in 'seq 1 $numOfRGBfiles'; do
#Insert code here that finds the file
#name of each rgb file and stores it 
#in $filename.

'find . -name "*.rgb" | sort | sed -n 
\$i\p |  xargs -I {} convert {} 
  • In my system, convert can take two arguments(e.g. convert inputImage.rgb outputImage.png ) – jjf Nov 5 '16 at 16:46
  • In my code, covert is taking just one input file for every for loop iteration. – jjf Nov 5 '16 at 16:53
  • It is not good practice to do seq 1 $numOfRGBfiles. It is better to do for all. The problem is that a file can disappear while the loop is running, or a new can occur. – hschou Nov 6 '16 at 23:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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