run program multiple times using one line shell co

2019-07-11 08:12发布

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条回答
Lonely孤独者°
2楼-- · 2019-07-11 08:36

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
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-11 08:55

Using GNU Parallel you can do:

parallel ./gif2bmp -i {} -o {.}.bmp ::: *.gif

The added benefit is that it will run one job for each cpu core in parallel.

Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial (http://www.gnu.org/software/parallel/parallel_tutorial.html). You command line with love you for it.

查看更多
登录 后发表回答