Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 批次偵測與轉換非 UTF-8 編碼檔案:enca 與 find 實戰指令

編輯:關於 Linux

前置準備

sudo apt-get install enca   # Debian/Ubuntu
# 或 yum install enca       # CentOS/RHEL

指令一:尋找非 UTF-8 檔案(以 .php 為例,排除 .svn)

find . ! -iregex '.*\.svn.*' -type f -name '*.php' \
  -exec bash -c "enca -L zh_CN {} | grep GB2312 > /dev/null && echo {}" \;

指令二:批次轉換為 UTF-8

find . ! -iregex '.*\.svn.*' -type f -name '*.php' \
  -exec bash -c "enca -L zh_CN {} | grep GB2312 > /dev/null && enconv -L zh_CN -x UTF-8 {}" \;

注意事項

純英文檔案 UTF-8 與 ASCII 相同,enca 會辨識為 ASCII,轉換指令會略過,屬正常現象。

結語

善用 find + enca + enconv 組合,即可自動化處理專案中混雜編碼的檔案,統一為 UTF-8。

Copyright © Windows教程網 All Rights Reserved