views:

263

answers:

1

I have the following gifs on my linux system:

$ find . -name *.gif
./gifs/02.gif17.gif
./gifs/fit_logo_en.gif
./gifs/halloween_eyes_63.gif
./gifs/importing-pcs.gif
./gifs/portal.gif
./gifs/Sunflower_as_gif_small.gif
./gifs/weird.gif
./gifs2/00p5dr69.gif
./gifs2/iss013e48788.gif
...and so on

What I have written is a program that converts GIF files to BMP with the following interface:

./gif2bmp -i inputfile -o outputfile

My question is, is it possible to write a one line command using xargs, awk, find etc. to run my program once for each one of these files? Or do I have to write a shell script with a loop?

+2  A: 

For that kind of work, it may be worth looking at find man page, especially the -exec option.

You can write something along the line of:

find . -name *.gif -exec gif2bmp -i {} -o {}.bmp \;

You can play with combinations ofdirname and basename to obtain better naming for the output file, though in this case, I would prefer to use a shell for loop, something like:

for i in `find . -name "*.gif"`; do
  DIR=`dirname $i`
  NAME=`basename $i .gif`
  gif2bmp -i $i -o ${DIR}/${NAME}.bmp
done
tonio
Thanks, this is exactly what I was looking for. I ended up using the following command:find . -name *.gif -exec ./convert -i {} -o {}.bmp \;The only problem with this is that my output file have extension .bmp.gif, but at least it works :)
teehoo
Good one-liner tonio. The .gif.bmp issue is why I'll usually go for the extra lines to pull out the base filename and change the extension.
Stephen P
@teehoo: the answer to the suffix problem is to write a shell script to cover the converter: `exec ./gif2bmp -i "$1" -o "${1%.gif}.bmp"`. You then run that from `find`: `find . -name '*.gif' -exec ./wrapper {} \;`.
Jonathan Leffler