Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use xargs to replace 2 arguments

Tags:

bash

shell

sh

I would like to write a script to convert svg files in a directory to png using svgexport cli

svgexport input.svg input.jpg

How can I use find and xargs -I {} to find and print out the svg files using the following:

find . -iname -0 "*.svg" | xargs -I {} svgexport {} ???

How can I fill-in the second argument by using the first argument and replacing .svg with .jpg?

like image 256
Stalin Kay Avatar asked Sep 13 '16 21:09

Stalin Kay


People also ask

Does xargs run in parallel?

xargs will run the first two commands in parallel, and then whenever one of them terminates, it will start another one, until the entire job is done. The same idea can be generalized to as many processors as you have handy. It also generalizes to other resources besides processors.

What is the point of xargs?

xargs (short for "extended arguments") is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

What does piping to xargs do?

xargs is a Unix command used to build and execute commands from the standard input. You can combine it with other powerful Unix commands, such as grep , awk , etc., by using pipes. In simple terms, it passes the output of a command as the input of another. It works on most Unix-like operating systems.


1 Answers

I think it's best to do this with a while loop:

find . -iname "*.svg" -print0 |
    while IFS= read -r -d '' file; do
        svgexport "$file" "${file%.svg}.jpg"
    do
like image 83
redneb Avatar answered Oct 27 '22 01:10

redneb