shell脚本,怎么查找项目中的重复图片

212 阅读1分钟

要在 iOS 项目中使用 Shell 脚本查找重复图片,可借助计算图片文件哈希值的方法,将哈希值相同的图片判定为重复图片

#!/bin/bash# 在这里指定 iOS 项目的目录
project_dir="/path/to/your/ios/project"# 检查指定的项目目录是否存在
if [ ! -d "$project_dir" ]; then
    echo "指定的 iOS 项目目录 $project_dir 不存在。"
    exit 1
fi# 临时文件用于存储所有图片的 MD5 哈希值和文件路径
temp_file="/tmp/image_md5_list.txt"# 临时文件用于存储重复图片分组信息
duplicate_group_file="/tmp/duplicate_image_groups.txt"# 清空临时文件
> $temp_file
> $duplicate_group_file# 查找 Assets.xcassets 中的图片
find "$project_dir" -type d -name "Assets.xcassets" | while read -r assets_dir; do
    find "$assets_dir" -type f ( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" ) | while read -r image_file; do
        md5=$(md5sum "$image_file" | awk '{print $1}')
        echo "$md5 $image_file" >> $temp_file
    done
done# 查找普通文件夹中的图片
find "$project_dir" -type f ( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" ) ! -path "*/Assets.xcassets/*" | while read -r image_file; do
    md5=$(md5sum "$image_file" | awk '{print $1}')
    echo "$md5 $image_file" >> $temp_file
done# 使用 awk 找出重复的图片并分组
awk '{
    md5=$1; path=$2;
    group[md5]=group[md5] (group[md5]==""? "" : ",") path;
}
END {
    for (m in group) {
        split(group[m], paths, ",");
        if (length(paths) > 1) {
            print group[m];
        }
    }
}' $temp_file | sort >> $duplicate_group_file# 输出重复图片的分组信息及临时文件路径
echo "重复的图片分组信息已记录到以下临时文件中:"
echo "$duplicate_group_file"
echo "文件内容如下:"
cat $duplicate_group_file# 清理临时文件
rm $temp_file
  1. 为脚本添加执行权限:chmod +x find_duplicate_images.sh
  2. 运行脚本:./find_duplicate_images.sh