linux批量修改檔案字元集shell指令碼

來源:互聯網
上載者:User

linux批量修改檔案字元集shell指令碼 需求:需要將 “/home/zichen/dxzs0305”目錄下所有PHP檔案字元集由 UTF-8
轉換為GB2312  www.2cto.com  shell指令碼內容:[html] #!/bin/bash -x  SUBFIX="php"  #需要轉換的目標檔案尾碼    www.2cto.com  cd /home/zichen/dxzs0305     #目標檔案根目錄    if [ -z $1 ];then      cd $PWD  else      if [ -d $1 ];then          cd $1      else          echo " $1 is not exist;"          exit 1      fi  fi    for i in $SUBFIX;  do      files=`find . -name "*.$i"`      for f in $files;      do          type=`file $f|awk -F':' '{print $2}' |awk  '{print $1}'` #擷取檔案類型          if [ $type != "ISO-8859" ];then              iconv -f UTF-8 -t GB2312 -o $f $f   #使用  iconv函數進行轉換             #  enca -L UTF-8 -x zh_cn $f   #如果使用enca 進行轉換,需要安裝 enca    命令 sudo apt-get  install  enca           else              echo "$f encoding is :$type"          fi        done  done   iconv 函數參數簡介:iconv命令用於轉換指定檔案的編碼,預設輸出到標準輸出裝置,亦可指定輸出檔案。用法: iconv [選項...] [檔案...]有如下選項可用:  www.2cto.com  輸入/輸出格式規範:-f, --from-code=名稱 原始文本編碼-t, --to-code=名稱 輸出編碼資訊:-l, --list 列舉所有已知的字元集輸出控制:-c 從輸出中忽略無效的字元-o, --output=FILE 輸出檔案-s, --silent 關閉警告--verbose 列印進度資訊-?, --help 給出該系統求助列表--usage 給出簡要的用法資訊-V, --version 列印程式版本號碼 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.