Windows 預設編碼 GBK/GB2312,檔案移至 Linux 常出現亂碼。本文介紹 enca 查看編碼、單檔轉換,並提供遞迴批量轉換腳本。
sudo apt-get install enca
enca -L zh_CN 檔案:查看編碼enca -L zh_CN -x UTF-8 檔案:就地轉碼為 UTF-8enca -L zh_CN -x UTF-8 < 檔案1 > 檔案2:另存新檔不覆蓋原檔#!/bin/bash
# 將指定目錄下檔案編碼遞迴轉為 UTF-8
function change_file_encoding() {
for file in $(ls -l | awk '{print $9}'); do
if [[ -d "$file" && $1 = "y" ]]; then
cd "$file"; echo "$file"; change_file_encoding "$1"; cd ..
elif [[ -f "$file" ]]; then
echo "$file"; enca -L zh_CN -x UTF-8 "$file"
fi
done
}
read -p "please enter the dir path: " path
if [[ ! -x "$path" ]]; then echo "dir path not exists"; exit; fi
read -p "please enter if you want to recursive? y/n: " recur
cd "$path"
if [[ $recur = "y" ]]; then change_file_encoding "y"; else change_file_encoding "n"; fi
使用方式:chmod +x encoding.sh && ./encoding.sh,依提示輸入路徑與是否遞迴。

解決跨平台編碼亂碼,從單檔到批量一次搞定。
Ubuntu 更新後 WiFi 圖示異常(扇形無法掃描)?一鍵還原軟體源解決
Kali Linux 網路配置完整指南:靜態 IP、DNS、網路重啟與校園網實戰案例
Ubuntu 编译安装 Hypertable 完整指南:依赖准备、CMake 配置与构建
Ubuntu 終端機安裝 PPA 軟體源完整圖文教學:add-apt-repository 實戰
老舊電腦翻新指南:安裝輕量級 Linux 系統實現基本辦公與多媒體需求
Linux 終端機新手必學 5 大效率技巧:Tab 補全、管道、萬用字元、歷史指令、快捷鍵
Linux SSH 連線自動斷開問題解決:ClientAliveInterval、ServerAliveInterval、TCPKeepAlive 三層保活機制完整配置