Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash How reuse found files name?

Tags:

bash

I want to run a script in a directory in order to generate an svg file for each found dot file. Something like:

find . -name "*.dot" -exec dot -Tsvg \{} \;

This works fine but just output the result on stdout. usually I am using a redirection to generate the svg file. How can I get the dot file name to use it in the redirection like

find . -name "*.dot" -exec dot -Tsvg > "$dotfilename".svg \{} \;
like image 967
Manuel Selva Avatar asked Dec 09 '22 22:12

Manuel Selva


1 Answers

The following:

for i in `find . -name "*.dot"`; do
   dot -Tsvg $i > $i.svg
done

performs the find (in backticks) and loops over the results, executing dot for each one. The filename is in $i.

This uses backtick substitution and is a useful mechanism for capturing command output for subsequent use in another command.

To remove the extension and add another, use ${i%.*}.svg. See this SO answer for more info.

like image 59
Brian Agnew Avatar answered Jan 06 '23 03:01

Brian Agnew