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 批量轉換文件編碼:iconv 遞歸腳本與現代工具對比

編輯:關於 Linux

歷史項目遷移、跨平台協作、舊系統數據導入常面臨 GBK/Big5/EUC-JP 到 UTF-8 的編碼轉換需求。本文提供基於 iconv 的遞歸 Shell 腳本,並對比 convmv、enca、Python chardet 等現代工具鏈。

編碼轉換流程圖

核心遞歸腳本 batch_convert.sh

#!/bin/bash
# 批量轉換指定目錄下所有文件編碼
# 用法: ./batch_convert.sh 源目錄 目標目錄 源編碼 目標編碼
# 示例: ./batch_convert.sh ./src ./dist GBK UTF-8

set -euo pipefail

SRC_DIR="${1:-.}"
DST_DIR="${2:-./converted}"
FROM_ENC="${3:-GBK}"
TO_ENC="${4:-UTF-8}"

mkdir -p "$DST_DIR"

# 遞歸查找文件(排除二進制、.git、構建產物)
find "$SRC_DIR" -type f \
    ! -path "*/.git/*" \
    ! -path "*/node_modules/*" \
    ! -path "*/__pycache__/*" \
    ! -name "*.pyc" \
    ! -name "*.class" \
    ! -name "*.o" \
    ! -name "*.so" \
    -print0 | while IFS= read -r -d '' file; do

    # 計算相對路徑並創建目標目錄
    rel_path="${file#$SRC_DIR/}"
    dst_file="$DST_DIR/$rel_path"
    mkdir -p "$(dirname "$dst_file")"

    # 檢測是否為文本文件(跳過二進制)
    if file -b "$file" | grep -q "text"; then
        echo "Converting: $file -> $dst_file"
        iconv -f "$FROM_ENC" -t "$TO_ENC" -c "$file" > "$dst_file" 2>/dev/null || {
            echo "WARN: Failed to convert $file (maybe already $TO_ENC or binary)"
            cp "$file" "$dst_file"
        }
    else
        echo "Skipping binary: $file"
        cp "$file" "$dst_file"
    fi
done

echo "Conversion completed. Output: $DST_DIR"

關鍵參數解析

參數說明常用值
-f源編碼GBK、GB2312、BIG5、EUC-JP、ISO-8859-1、CP936
-t目標編碼UTF-8、UTF-16LE
-c忽略無法轉換字符(防止中斷)生產環境必加
//TRANSLIT近似轉換(如無法轉換用相似字符)-t UTF-8//TRANSLIT
//IGNORE直接丟棄無法轉換字節-t UTF-8//IGNORE

編碼自動檢測與批量轉換(Python 方案)

#!/usr/bin/env python3
# auto_convert.py: 自動檢測編碼並轉 UTF-8
import os, sys, chardet

def convert_file(filepath, target_enc='UTF-8'):
    with open(filepath, 'rb') as f:
        raw = f.read(10000)  # 采樣檢測
    result = chardet.detect(raw)
    src_enc = result['encoding']
    confidence = result['confidence']

    if not src_enc or confidence < 0.7:
        print(f"SKIP (low confidence): {filepath} -> {src_enc} ({confidence})")
        return

    if src_enc.lower() == target_enc.lower():
        print(f"SKIP (already {target_enc}): {filepath}")
        return

    try:
        content = raw.decode(src_enc)
        # 讀取完整文件(大文件需分塊)
        with open(filepath, 'r', encoding=src_enc) as f:
            full = f.read()
        with open(filepath + '.utf8', 'w', encoding=target_enc) as f:
            f.write(full)
        print(f"CONVERTED: {filepath} ({src_enc} -> {target_enc})")
    except Exception as e:
        print(f"ERROR: {filepath} - {e}")

if __name__ == '__main__':
    root = sys.argv[1] if len(sys.argv) > 1 else '.'
    for dirpath, _, filenames in os.walk(root):
        for f in filenames:
            if f.endswith(('.txt', '.csv', '.sql', '.java', '.cpp', '.h', '.py', '.js', '.html', '.css')):
                convert_file(os.path.join(dirpath, f))

文件名編碼轉換(convmv)

# 安裝
yum install convmv -y  # 或 apt install convmv

# 預覽(不實際修改)
convmv -f GBK -t UTF-8 -r /path/to/dir --notest

# 實際執行
convmv -f GBK -t UTF-8 -r /path/to/dir

工具鏈選型建議

需求推薦工具理由
已知源編碼、批量文本轉換iconv + find/xargs標準、高效、可控
未知源編碼、需自動檢測Python chardet / enca智能檢測、可整合腳本
文件名亂碼修復convmv專門處理文件名編碼
大文件(>1GB)流式轉換iconv 管道 / Python 分塊讀寫避免記憶體溢出
CI/CD 流水線集成iconv (標準庫) / Docker 映像預裝無外部依賴、跨平台

總結

編碼轉換的核心是 「檢測 → 驗證 → 轉換 → 校驗」 閉環。iconv 仍是批量文本轉換的基石,**加上 -c 忽略錯誤、//TRANSLIT 近似轉換、二進制文件跳過** 三大防護,配合 chardet 自動檢測未知編碼,可構建健壯的編碼遷移流水線。記住:轉換前務必備份,轉換後抽樣驗證。

Copyright © Windows教程網 All Rights Reserved