bash - 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.
解决方案
请你试试:
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
依赖于分词的循环,尤其是在处理文件名时。请考虑文件名和路径名可能包含空格、换行符、制表符或其他特殊字符。
推荐阅读
- javascript - 如何在我的 Web 应用程序中呈现以下 TeX (LaTeX) HTML 字符串?
- windows - 如何获取窗口相对于该窗口的非客户端矩形的客户端矩形偏移量?
- c# - 如何在 Linq Where 子句中将 Char 转换为 Datetime
- c++ - 使用 Composite Pattern 制作游戏菜单
- bazel - 如何在 Bazel 中删除缓存的下载对象(例如从 http_archive)?
- javascript - componentDidUpdate(prevProps, prevState, snapshot): prevProps 未定义
- wordpress - 单品页面和轮播第一张图片错误的产品图片
- c# - 使用 SimpleInjector 在 Attribute 中注入服务
- arrays - 如何使用 mongo shell 获取 MongoDB 集合中的所有字段名称,包括嵌套字段名称?
- python - 查找 GeoTiff 图像中每个像素的纬度/经度坐标