ubuntu 批量编码转换

  • 查询编码

    find . \( -name "*.cpp" -or -name "*.c" -or -name "*.h" \) -type f -exec file -i {} \; > /tmp/a
    
    
  • 转换

    find . -name "*" -type f -exec iconv -f GBK -t UTF-8 {} -o /home/share/jni/{} \;
    
    
  • 转换脚本

    #!/bin/sh
    FILES=$(find . -type f -name '*.*')
    for f in $FILES
    do
        if test -f $f; then
            CHARSET="$( file -bi "$f"|awk -F "=" '{print $2}')"
            echo $CHARSET
            if [ "$CHARSET" = iso-8859-1 ]; then
                echo "Converting $f from $CHARSET to utf-8"
                # iconv -f "$CHARSET" -t utf-8 "$f" -o "$f.temp"
                iconv -f GBK -t UTF-8 "$f" -o "$f.temp"
                mv -f "$f.temp" $f
            fi
        else
            echo "Skipping $f - it's a regular file";
        fi
    done
    
  • 参考链接