如何使用sed在这个shell命令行中更改目标目录



我使用此命令行查找目录中的所有SVG(数千(,并使用Inkscape将它们转换为PNG。效果很好。这是我的问题。它输出同一目录中的PNG。我想更改目标目录。

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

看起来$i是file_path+file_name,sed对文件扩展名进行搜索/替换。如何搜索/替换我的文件路径?或者有更好的方法在这个命令行中定义不同的目标路径吗?

非常感谢您的帮助。

请尝试一下:

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循环,尤其是在处理文件名时。请考虑文件名和路径名可能包含空格、换行符、制表符或其他特殊字符。

或者带有一行(为了可读性而拆分(

find /home/wyatt/test/svgsDIR -name "*.svg" |
xargs -I{} sh -c 'inkscape "{}" --export-background-opacity=0 --export-png='$destdir'/$(basename {} .svg).png -w 700' 

可能与查找内置exec:一起工作

find /home/wyatt/test/svgsDIR -name "*.svg" -exec sh -c 'inkscape "{}" --export-background-opacity=0 --export-png='$destdir'/$(basename {} .svg).png -w 700' ;

或者将目标目录作为参数传递,以简化引用。

find /home/wyatt/test/svgsDIR -name "*.svg" -exec sh -c 'inkscape "$1" --export-background-opacity=0 --export-png="$2/$(basename $1 .svg).png" -w 700' '{}' "$targetdir"  ;

最新更新