歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

linux批量轉換整個目錄下的文件編碼為UTF-8

#!/bin/bash - 
#===============================================================================
#
#          FILE: conv.sh
# 
#         USAGE: ./conv.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: 目前不支持傳入參數中含有空格;
#         NOTES: ---
#        AUTHOR: linkscue (scue), [email protected]
#       CREATED: 2013年03月06日 22時52分31秒 HKT
#     COPYRIGHT: Copyright (c) 2013, linkscue
#      REVISION: 0.1
#  ORGANIZATION: ---
#===============================================================================

__ScriptVersion="0.1"

#===  FUNCTION  ================================================================
#         NAME:  usage
#  DESCRIPTION:  Display usage information.
#===============================================================================
function usage ()
{
        cat <<- EOT

  Usage :  $0 -s suffix1 -s suffix2 -d dir1 -d dir2 -f file1 -f file2

  Options:
  -h|help       Display this message
  -v|version    Display script version
  -s suffix     Setting suffix
  -d directory  Convert all file encoding to UTF-8
  -f file       Convert a file encoding to UTF-8

EOT
}    # ----------  end of function usage  ----------

#-----------------------------------------------------------------------
#  Handle command line arguments
#-----------------------------------------------------------------------

suffixs=()
directorys=()
files=()
while getopts ":hvd:f:s:" opt
do
  case $opt in

    h|help     )  usage; exit 0   ;;

    v|version  )  echo "$0 -- Version $__ScriptVersion"; exit 0   ;;

    f  )  files+=("$OPTARG")   ;;

    d  )  directorys+=("$OPTARG")   ;;

    s  )  suffixs+=("$OPTARG")   ;;

    \? )  echo -e "\n  Option does not exist : $OPTARG\n"
          usage; exit 1   ;;

  esac    # --- end of case ---
done
shift $(($OPTIND-1))

# 檢查輸入
if [[ ${#files} -lt 1 ]] && [[ ${#directorys} -lt 1 ]]; then
    usage
    exit
fi

TMPFILE="$(mktemp -t convXXXXXX)"
trap "rm -f '$TMPFILE'" 0               # EXIT
trap "rm -f '$TMPFILE'; exit 1" 2       # INT
trap "rm -f '$TMPFILE'; exit 1" 1 15    # HUP TERM

#-------------------------------------------------------------------------------
#  轉換編碼函數
#-------------------------------------------------------------------------------
conv_utf8(){
    file="$1"
    echo "處理文件: '$file' ..."
    iconv -f gb2312 -t UTF-8 "$file" -o $TMPFILE 2> /dev/null &&\
        mv -f $TMPFILE "$f" || {
        echo "轉換失敗: '${file}'"
    }
}

# 轉換文件
for f in "${files[@]}"; do
    conv_utf8 "$f"
done

# 轉換目錄文件
if [[ ${#directorys} -gt 1 ]]; then
    if [[ ${#suffixs} -lt 1 ]]; then
        echo
        echo "請指定需轉換編碼的文件後綴,如 '-s txt -s java'"
        echo
        usage
        exit
    else
        for s in "${suffixs[@]}"; do
            for f in $(find $directorys -type f -name "*.${s#.}"); do
                conv_utf8 "$f"
            done
        done
    fi
fi

 

假如把這個腳本內容保存至~/bin/conv,並給予權限;

使用舉例:

  1. conv -f file1 -f file2 -s java -s xml -d dir1 -d dir2

  2. conv -s java -d .

  3. conv -s java -s xml -d android_helloworld

  4. conv -f file1 -f file2

有了這個轉換編碼,把Windows下的工程引入到Linux上開發就輕松得多了,不再有什麼字符編碼讀不出來的問題;


Copyright © Linux教程網 All Rights Reserved