使用 Bash find 递归检查非正方形 jpg 图像尺寸,我非常接近

Using Bash find to recursively check non square jpg image dimensions, im very close

我正在尝试递归循环遍历所有目录并打印所有不完全正方形的 jpg 文件的完整路径列表。

这是我用来查找 2 到 6 层之间缺少 cover.jpg:

的目录的东西
find . -mindepth 2 -maxdepth 6 -type d '!' -exec test -e "{}/cover.jpg" ';' -print

然后我修改了它以尝试找到非方形图像:

find . -mindepth 2 -maxdepth 6 -type d '!' -exec identify -format '%[fx:(h == w)]' "{}/cover.jpg" ';'

上面非常接近工作,它根据是否为正方形的结果开始输出 0 和 1,但是 0 或 1 输出到同一行而不是新行,它只是继续在检查文件的同一行上。

1111111111111111111111111111111001100101101110111001110010101001011000001101

我在想如果它每行输出一个 1 或 0 我可以 grep 如果 0 并打印文件路径。

我没有很多 Bash 经验,到目前为止我运气不好。感谢任何帮助。

使用循环可能更容易。

find . -mindepth 2 -maxdepth 6 -type d '!' | while read file; do #Piping find results into loop

    identify -format '%[fx:(h == w)]' "$file/cover.jpg" ';' #For each file, it runs the command you gave
    printf "\n" #Prints a new line

done

根据 imagemagick 文档,我认为您可以将换行符(和路径)附加到格式中:

find . -mindepth 2 -maxdepth 6 -type d '!' -exec \
    identify -format '%[fx:(h == w)] %d/%f\n' "{}/cover.jpg" ';' |\
sed -n '/^0 / s///p'

(未经测试的代码,因为我没有安装 imagemagick。sed 命令实现 grep 并删除添加的前导零。)