论坛附件维护常用bash shell脚本

作者在 2026-01-22 22:44:04 发布以下内容
找出所有大于或等于源文件的缩略图
find . -type f ! -name '*.thumb.jpg' -print0 | while IFS= read -r -d '' src; do
  thumb="${src}.thumb.jpg"

  if [ -f "$thumb" ]; then
    src_size=$(stat -c '%s' "$src")
    thumb_size=$(stat -c '%s' "$thumb")

    if [ "$thumb_size" -ge "$src_size" ]; then
      echo "$thumb"
    fi
  fi
done

作者在 2026-01-22 23:00:44 补充以下内容
找出源文件已不在的缩略图
find . -type f -name '*.thumb.jpg' -print0 | while IFS= read -r -d '' thumb; do
  src="${thumb%.thumb.jpg}"

  if [ ! -f "$src" ]; then
    echo "$thumb"
  fi
done
找出源文件已不在的webp文件,不包括单纯的webp文件
find . -type f \
  -regextype posix-extended \
  -regex '.*\.[[:alnum:]]{2,5}\.webp$' \
  -print0 | while IFS= read -r -d '' webp; do
  src="${webp%.webp}"
  if [ ! -f "$src" ]; then
    echo "$webp"
  fi
done

作者在 2026-01-22 23:22:20 补充以下内容
为所有图片生成webp文件,只有webp文件更小才保留
find . -type f ! -iname '*webp*' -print0 | while IFS= read -r -d '' src; do
  webp="${src}.webp"

  # 已存在就跳过
  [ -f "$webp" ] && continue

  case "${src,,}" in
    *.jpg|*.jpeg|*.png)
      echo "cwebp: $src → $webp"
      cwebp -m 6 "$src" -o "$webp" >/dev/null 2>&1
      ;;
    *.gif)
      echo "gif2webp: $src → $webp"
      timeout 10s gif2webp -m 6 -lossy "$src" -o "$webp" >/dev/null 2>&1
      ;;
    *)
      # 其他文件类型跳过
      continue
      ;;
  esac

  # 检查 webp 是否比源文件小
  if [ -f "$webp" ]; then
    src_size=$(stat -c '%s' "$src")
    webp_size=$(stat -c '%s' "$webp")

    if [ "$webp_size" -ge "$src_size" ]; then
      echo "Remove $webp (larger than source)"
      rm -f "$webp"
    fi
  fi
done

作者在 2026-01-23 00:36:08 补充以下内容
仅检查大于或等于源文件的webp文件,打印出来
find . -type f ! -iname '*webp*' -print0 | while IFS= read -r -d '' src; do
  webp="${src}.webp"

  # 检查 webp 是否比源文件小
  if [ -f "$webp" ]; then
    src_size=$(stat -c '%s' "$src")
    webp_size=$(stat -c '%s' "$webp")

    if [ "$webp_size" -ge "$src_size" ]; then
      echo "$webp"
    fi
  fi
done

作者在 2026-01-23 00:51:17 补充以下内容
仅删除存在源文件的webp文件,单纯的webp文件不删除
find . -type f -name '*.webp' -print0 | while IFS= read -r -d '' webp; do
  # 去掉 .webp 后缀得到可能的源文件名
  src="${webp%.webp}"

  if [ -f "$src" ]; then
    echo "Delete $webp (source exists: $src)"
    rm -f "$webp"
  fi
done

作者在 2026-01-23 05:44:18 补充以下内容

———————— 头像维护相关 ————————

作者在 2026-01-23 05:45:56 补充以下内容
  • 按完整路径去掉后缀判断重复
  • 重复路径只保留最新文件(按修改时间)
  • 删除旧文件
declare -A file_groups

while IFS= read -r filepath; do
    # 去掉后缀
    path_no_ext="${filepath%.*}"

    # 累积文件列表,用空格分隔
    file_groups["$path_no_ext"]+="$filepath"$'\n'
done < <(find . -type f)

# 遍历每个重复组
for base_path in "${!file_groups[@]}"; do
    # 读取组内文件到数组
    IFS=$'\n' read -r -d '' -a files <<< "${file_groups[$base_path]}"$'\0'

    # 如果组内文件多于1个,处理
    if [ "${#files[@]}" -gt 1 ]; then
        # 找到最新修改时间的文件
        latest_file=""
        latest_mtime=0
        for f in "${files[@]}"; do
            mtime=$(stat -c %Y "$f")  # Linux 用 %Y,macOS 用 -f %m
            if [ "$mtime" -gt "$latest_mtime" ]; then
                latest_mtime=$mtime
                latest_file="$f"
            fi
        done

        echo "保留最新文件: $latest_file"
        # 删除其他文件
        for f in "${files[@]}"; do
            if [ "$f" != "$latest_file" ]; then
                echo "删除旧文件: $f"
                rm -f "$f"
            fi
        done
        echo ""
    fi
done

作者在 2026-01-23 06:00:53 补充以下内容
只压缩大于 300x300 的图片,用 convert 压缩到 300x300 并设置质量为 90,但如果压缩后文件比原文件大,就舍弃压缩文件。
#!/bin/bash
# 要处理的根目录
DIR="./"  # 可修改为你的目录
# 遍历所有图片文件(jpg, jpeg, png, gif),递归子目录
find "$DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | while IFS= read -r -d '' file; do
    ext="${file##*.}"
    ext="${ext,,}"
    # 获取图片宽高
    dimensions=$(identify -format "%w %h" "$file[0]" 2>/dev/null)
    if [ -z "$dimensions" ]; then
        echo "无法识别: $file"
        continue
    fi
    # 读取宽高
    width=$(echo "$dimensions" | awk '{print $1}')
    height=$(echo "$dimensions" | awk '{print $2}')
    # 确保宽高是整数
    if ! [[ "$width" =~ ^[0-9]+$ ]] || ! [[ "$height" =~ ^[0-9]+$ ]]; then
        echo "宽高非法: $file ($dimensions)"
        continue
    fi
    # 只处理宽高大于300的图片
    if [ "$width" -le 300 ] && [ "$height" -le 300 ]; then
        continue
    fi
    echo "处理: $file ($width x $height)"
    # 创建临时文件
    case "$ext" in
        gif)
            tmpfile=$(mktemp /tmp/compressed_XXXXXX.gif)
            ;;
        *)
            tmpfile=$(mktemp /tmp/compressed_XXXXXX.jpg)
            ;;
    esac
    # 压缩到 300x300,质量 90
    convert "$file" -resize 300x300\> -quality 90 "$tmpfile"
    # 检查压缩文件是否存在且大小 > 0
    tmp_size_bytes=$(stat -c%s "$tmpfile" 2>/dev/null | tr -d '[:space:]' | grep -o '[0-9]\+')
    if [ -z "$tmp_size_bytes" ] || [ "$tmp_size_bytes" -eq 0 ]; then
        echo "压缩文件无效或为空,保留原文件: $file"
        rm -f "$tmpfile"
        continue
    fi
    # 获取原文件大小
    orig_size=$(stat -c%s "$file" | tr -d '[:space:]' | grep -o '[0-9]\+')
    new_size=$tmp_size_bytes
    # 文件大小比较
    if [ -z "$orig_size" ]; then
        echo "获取原文件大小失败: $file"
        rm -f "$tmpfile"
        continue
    fi
    if [ "$new_size" -ge "$orig_size" ]; then
        # 压缩文件比原文件大或一样,舍弃
        rm -f "$tmpfile"
        echo "保留原文件: $file"
    else
        # 替换原文件
        mv "$tmpfile" "$file"
        percent=$(( (orig_size - new_size) * 100 / orig_size ))
        echo "替换文件: $file (原大小 $orig_size -> 新大小 $new_size, 节省 $percent%)"
    fi
done

论坛维护 | 阅读 760 次
文章评论,共1条
静夜思(作者)
2026-01-22 23:30
1
感受ai的强大魔力吧👍
游客请输入验证码
浏览3064427次
文章归档
最新评论
  • 静夜思:感受ai的强大魔力吧👍
  • 静夜思:-1是多核