首页 > 解决方案 > How can I use sed to change my target dir in this shell command line?

问题描述

I use this command line to find all the SVGs (thousands) in a directory and convert them to PNGs using Inkscape. Works great. Here is my issue. It outputs the PNGs in the same directory. I would like to change the target directory.

for i in `find /home/wyatt/test/svgsDIR -name "*.svg"`; do inkscape $i --export-background-opacity=0 --export-png=`echo $i | sed -e 's/svg$/png/'` -w 700 ; done

It appears $i is the file_path + file_name, and sed does a search/replace on the file extension. How do I search/replace my file_path? Or is there a better way to define a different target path within this command line?

Any help is much appreciated.

标签: bashshellsed

解决方案


请你试试:

destdir="DIR"   # replace with your desired directory name
mkdir -p "$destdir"
find /home/wyatt/test/svgsDIR -name "*.svg" -print0 | while IFS= read -r -d "" i; do
    destfile="$destdir/$(basename -s .svg "$i").png"
    inkscape "$i" --export-background-opacity=0 --export-png="$destfile" -w 700
done

或者

destdir="DIR"
mkdir -p "$destdir"
for i in /home/wyatt/test/svgsDIR/*.svg; do
    destfile="$destdir/$(basename -s .svg "$i").png"
    inkscape "$i" --export-background-opacity=0 --export-png="$destfile" -w 700
done

这可能是题外话,但不建议使用for依赖于分词的循环,尤其是在处理文件名时。请考虑文件名和路径名可能包含空格、换行符、制表符或其他特殊字符。


推荐阅读