[Linux] 批量轉換整個目錄下的檔案編碼為UTF-8;

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   os   ar   使用   

[Linux] 批量轉換整個目錄下的檔案編碼為UTF-8;

#!/bin/bash - #===============================================================================##          FILE: conv.sh# #         USAGE: ./conv.sh # #   DESCRIPTION: 一個支援把整個目錄遞迴轉換GB2312為UTF-8的指令碼;# #       OPTIONS: ---#  REQUIREMENTS: Linux核心的作業系統;#          BUGS: 目前不支援傳入參數中含有空格;#         NOTES: 輸入支援三種格式,隨你的心意而定製;#        AUTHOR: linkscue (scue), [email protected]#       CREATED: 2013年03月06日 22時52分31秒 HKT#     COPYRIGHT: Copyright (c) 2013, linkscue#      REVISION: 0.1#  ORGANIZATION: ---#===============================================================================set -o nounset                              # Treat unset variables as an error#-------------------------------------------------------------------------------#  檢查錯誤#-------------------------------------------------------------------------------if [[ $# == 0 ]] ; then    echo ""    echo "程式版本:0.1"    echo "實現功能:批量轉換GB2312字元編碼至UTF-8;"    echo "使用方法:$(basename $0) <尾碼名> <檔案1> <檔案2> <目錄1> <目錄2> .."    echo "操作提示:"    echo "    1. 尾碼名不需要‘.‘這個符號;"    echo "    2. 當輸入參數中無尾碼名,將從傳入檔案中取尾碼並提示是否進一步操作;"    echo ""    exit 1fi#-------------------------------------------------------------------------------#  傳入參數情形1#  傳入的是第一個參數是尾碼名;#  判斷的依據是第一個參數傳來的不是一個檔案;#-------------------------------------------------------------------------------if [[ ! -f $1 ]] ; then    suffix=$1fitarget=${@:2:$#}for n in ${target[@]};do    # 判斷是否是一個檔案;    if [[ -f $n ]] ; then        iconv -f gb2312 -t UTF-8 $n -o $n 2> /dev/null    fi    #判斷是否是一個目錄    if [[ -d $n ]] ; then            find "$n" -name "*.$suffix" | while read line ; do            iconv -f gb2312 -t UTF-8 "$line" -o "$line"  2> /dev/null        done    fidone#-------------------------------------------------------------------------------#  傳入參數情形2#  傳入的第一個參數是一個檔案;#  若傳入參數中無目錄時,將直接把檔案轉碼;#  若傳入參數中有目錄時,將詢問是否以第一個參數尾碼作為搜尋目錄條件;#-------------------------------------------------------------------------------if [[ -f $1 ]] ; then    # 判斷傳入參數中是否有目錄;    for n in [email protected] ; do        if [[ -d "$n" ]]; then            HAS_DIR=true        fi    done    # 當傳入參數中沒有目錄時,直接把傳入檔案轉碼;    if [[ $HAS_DIR != "true" ]]; then        for n in [email protected] ; do            iconv -f gb2312 -t UTF-8 "$n" -o "$n" 2> /dev/null        done    else        # 當傳入參數中含有目錄時,將使用第一個傳入參數的尾碼;        suffix=${1##*.}        if [[ $suffix != "" ]]; then            read -p ">> 發現第一個傳入參數的尾碼名為$suffix,是否使用它作為搜尋目錄的條件?[y/N]"            if [[ $REPLY == "y" ]]; then                for n in [email protected] ; do                    if [[ -f "$n" ]]; then                        iconv -f gb2312 -t UTF-8 "$n" -o "$n" 2> /dev/null                    fi                    if [[ -d "$n" ]]; then                        find "$n" -name "*.$suffix" | while read line ; do                        iconv -f gb2312 -t UTF-8 "$line" -o "$line"  2> /dev/null                    done                    fi                done            fi        fi    fifi#-------------------------------------------------------------------------------#  傳入參數情形3#  傳入第一個參數是目錄;#  這時考慮到使用者可能轉換整個目錄,卻忘記輸轉換檔尾碼名;#  此時將會提示使用者輸入尾碼名;#  可以輸入多個尾碼名,比如Android的程式原始碼含有xml與java尾碼;#-------------------------------------------------------------------------------if [[ -d $1 ]]; then    [email protected]    # 提示使用者輸入檔案的尾碼名;    read -p ">> 發現尚未輸入要轉換的檔案尾碼名,請輸入需轉碼的尾碼[可輸入多個]:" suffix_3     for n in ${argvs[@]};do        # 處理的是一般檔案;         if [[ -f "$n" ]]; then            iconv -f gb2312 -t UTF-8 "$n" -o "$n" 2> /dev/null        fi        # 處理的是一個目錄;        if [[ -d "$n" ]]; then            for suffix in ${suffix_3[@]} ; do                find "$n" -name "*.$suffix" | while read line ; do                    iconv -f gb2312 -t UTF-8 "$line" -o "$line" 2> /dev/null                done            done        fi    donefi

操作說明:

假如把這個指令碼內容儲存至~/bin/conv,並給予許可權;

使用舉例:

1. conv <尾碼> <檔案1> <檔案2> <目錄1> <目錄2> <目錄或檔案..>

conv java aaa.java bbb.java Android原始碼目錄1/  Android原始碼目錄2/

2. conv <檔案1> <檔案2> <目錄1> <目錄2> <目錄或檔案..>   # 注意:將以第一個檔案尾碼作為目錄的搜尋條件

conv aaa.java bbb.java Android原始碼目錄1/  Android原始碼目錄2/

3. conv <目錄1>  <目錄2> <目錄或檔案>           # 注意:這裡將會提示輸入尾碼名(可輸入多個尾碼名)

conv Android原始碼目錄1/  Android原始碼目錄2/ aaa.java bbb.java>> 發現尚未輸入要轉換的檔案尾碼名,請輸入需轉碼的尾碼[可輸入多個]:xml java


[Linux] 批量轉換整個目錄下的檔案編碼為UTF-8;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.