最近有迁移个人存储内容到DSM的需求,本来服务器上使用的是CloudReve。
其文件夹结构和文件名都与上传时候的版本有诸多差异。
写了这个小工具来帮助解决,敬请使用。

#!/bin/bash

CLEAN_EMPTY=0
CLEAN_THUMB=0
CLEAN_FILENAME=0

while [ "$#" -gt 0 ]; do
    case $1 in
        --clsempty) CLEAN_EMPTY=1 ;;
        --clsthumb) CLEAN_THUMB=1 ;;
        --clsfilename) CLEAN_FILENAME=1 ;;
        *) break ;;
    esac
    shift
done

if [ "$#" -ne 1 ]; then
    echo "Cloudreve Cleaner 1.0"
    echo "Usage: ./clscrve.sh [--clsempty] [--clsthumb] [--clsfilename] <directory>"
    exit 1
fi

DIR=$1

if [ ! -d "$DIR" ]; then
    echo "Directory not found: $DIR"
    exit 1
fi

if [ "$CLEAN_FILENAME" -eq 1 ]; then
    echo "Cleaning filenames..."
    find "$DIR" -type f | while read -r file; do
        dir=$(dirname "$file")
        base=$(basename "$file")
        originname=$(echo "$base" | awk -F'_' '{ $1=$2=""; sub(/^  /,""); print }')
        if [ "$base" == "$originname" ]; then
            continue
        fi
        mv -n "$file" "$dir/$originname"
        echo "${base} -> ${originname}"
    done
fi

if [ "$CLEAN_EMPTY" -eq 1 ]; then
    echo "Cleaning empty directories..."
    find "$DIR" -type d -empty -delete
fi

if [ "$CLEAN_THUMB" -eq 1 ]; then
    echo "Cleaning thumbnail files..."
    find "$DIR" -type f -name '*. thumb' -delete
fi

echo "SUCCESS IN $(date '+%Y-%m-%d %H:%M:%S')"
评论