Run the program multiple times using the command line command
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
I wrote 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 one line using xargs, awk, find, etc. to run my program once for each of these files? Or do I need to write a wrapper script with a loop?
a source to share
For this kind of work it might be worth looking at the man page find
, especially the option -exec
.
You can write something line by line:
find . -name *.gif -exec gif2bmp -i {} -o {}.bmp \;
You can play with combinations of dirname
and basename
to get a better name for the output file, although in this case I'd rather use a shell loop for
, something like:
for i in `find . -name "*.gif"`; do
DIR=`dirname $i`
NAME=`basename $i .gif`
gif2bmp -i $i -o ${DIR}/${NAME}.bmp
done
a source to share
Using GNU Parallel, you can:
parallel ./gif2bmp -i {} -o {.}.bmp ::: *.gif
An added benefit is that it will execute one job for each processor core in parallel.
Watch the introductory video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
Follow the tutorial ( http://www.gnu.org/software/parallel/parallel_tutorial.html ). You control the command line with love for it.
a source to share