Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 檔案字元編碼查看與轉換:vim、file、iconv 實戰

編輯:Linux 系統常見問題解答

查看檔案編碼

方法指令輸出範例
vim:set fileencodingfileencoding=utf-8
filefile -i filenametext/plain; charset=utf-8
encaenca -L zh_CN filenameUTF-8 (需安裝 enca)
uchardetuchardet filenameUTF-8 (需安裝 uchardet)

iconv 編碼轉換

# 基本語法
iconv -f FROM_ENCODING -t TO_ENCODING inputfile -o outputfile

# 常用範例
iconv -f UTF-8 -t UTF-16LE input.txt -o output.txt
iconv -f BIG5 -t UTF-8 legacy.txt -o modern.txt
iconv -f GBK -t UTF-8 cn.txt -o cn_utf8.txt

# 批次轉換
for f in *.txt; do
  iconv -f GBK -t UTF-8 "$f" -o "${f%.txt}_utf8.txt"
done

查看支援編碼清單

iconv --list

常見編碼名稱對照

中文常用名iconv 標準名
UTF-8UTF-8
UTF-16 Little EndianUTF-16LE
UTF-16 Big EndianUTF-16BE
繁體中文 Big5BIG5 / BIG5-HKSCS
簡體中文 GBKGBK / GB18030
ISO-8859-1ISO-8859-1

進階:vim 另存新編碼

:set fileencoding=utf-8
:w newfile.txt

注意事項

  • 轉換前請備份原檔
  • 若來源編碼判斷錯誤,輸出會產生亂碼;建議先用 file -i 或 uchardet 確認
  • 含 BOM 的 UTF-8 在 iconv 中視為 UTF-8,輸出預設不含 BOM
Copyright © Windows教程網 All Rights Reserved