linux - 在子文件夹中批量转换图像文件 jpg、png 到 webp
问题描述
我找到了一个脚本来转换目录中的文件,但我需要子目录
你能帮助我吗 ?
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
# cd to the directory of the image so we can work with just filenames
dir="$(dirname "$1")"
cd "$dir" || exit 1
base="$(basename "$1" .png)"
# create a WebP version of the PNG
cwebp -q 80 "$base".png -o "$base".webp
# delete the WebP file if it is equal size or larger than the original PNG
if [[ `stat -c '%s' "$base".webp` -ge `stat -c '%s' "$base".png` ]]; then
echo "Deleting WebP file that is no smaller than PNG"
rm -f "$base".webp
fi
# delete the WebP file if it is size 0
if [[ -f "$base".webp && ! -s "$base".webp ]]; then
echo "Deleting empty WebP file"
rm -f "$base".webp
fi
解决方案
好消息:您不必更改脚本!
您可以通过以下命令找到根目录中的所有目录:
find /path/to/root/dir -type d
您可以为每个找到的目录添加一些命令的执行:
假设您的脚本名称是script.sh并且它位于您的主目录中,并且您希望在当前目录下的所有子目录上运行它(包括当前目录):
find . -type d -exec ~/script.sh "{}" \;
推荐阅读
- c++ - 无法读取内存,类中的私有
- python - matchMask 必须与函数'drawMatches 中的matches1to2 具有相同的大小
- sql - Oracle 查询 - 将每个类别的总数添加到查询中
- nginx - 如何以 DRY 方式在包含共享路径的 2 个应用程序之间进行代理
- spring-boot - Pivotal Cloud Foundry - 由于 java netsocket(port) 权限被拒绝,部署的 Spring Boot 应用程序无法运行
- firebase - Flutter firebase_database - DatabaseReference runTransaction - 同时交易 - 两者都通过
- powershell - 为什么字符串显示长度为 1?
- r - R - 基于来自另一列的部分字符串匹配添加新列
- sql - 从日期范围生成销售事实数据
- r - 基于条件 R 的观测子集问题