Recursively rename files to ASCII Standard

前端 未结 2 401
醉酒成梦
醉酒成梦 2021-01-22 15:56

So we have a problem where we need to crawl through hundreds of thousands of images and rename all of them to comply with ASCII standards. After doing a lot of research online,

2条回答
  •  隐瞒了意图╮
    2021-01-22 16:56

    Put this in a shell script, say fixname.sh:

    #!/bin/sh
    
    dir=$(dirname "$1")
    name=$(basename "$1")
    
    newname=$(echo "$name" | sed -e 's/[^A-Za-z0-9._-]/_/g')
    if [ "$name" != "$newname" ]; then
        if [ ! -e "$newname" ]; then
            mv "$1" "$dir/$newname"
        else
            echo >&2 "$newname already exist for $1"
        fi
    fi
    

    Then use find like so:

    find . -type f -exec sh fixname.sh {} \;
    

提交回复
热议问题