支持100+字符编码
字符集间相互转换
解决乱码问题
批量转换文件编码
iconv [选项] -f 源编码 -t 目标编码 [输入文件]
iconv -l
-f是from(从什么编码),-t是to(到什么编码)。
| 选项 | 说明 |
|---|---|
-f 编码 或 --from-code=编码 |
指定源文件编码(必须) |
-t 编码 或 --to-code=编码 |
指定目标编码(必须) |
-l 或 --list |
列出所有支持的编码 |
-c |
忽略无效字符,继续转换 |
-o 文件 或 --output=文件 |
输出到指定文件(默认输出到stdout) |
-s 或 --silent |
静默模式,不输出警告 |
--verbose |
详细模式,显示处理信息 |
--unicode-subst=格式 |
替换无法转换的Unicode字符 |
--byte-subst=格式 |
替换无法转换的字节序列 |
--widechar-subst=格式 |
替换无法转换的宽字符 |
--help |
显示帮助信息 |
--version |
显示版本信息 |
| 编码名称 | 别名 | 说明 |
|---|---|---|
UTF-8 |
UTF8 | Unicode变长编码,现代标准 |
UTF-16 |
UTF16 | Unicode 16位编码 |
UTF-32 |
UTF32 | Unicode 32位编码 |
GB2312 |
GB2312-80 | 简体中文国家标准 |
GBK |
CP936 | GB2312扩展,含繁体字 |
GB18030 |
最新中文国家标准 | |
BIG5 |
BIG5-HKSCS | 繁体中文编码 |
ISO-8859-1 |
LATIN1 | 西欧语言编码 |
ISO-8859-15 |
LATIN9 | LATIN1扩展,含€符号 |
CP1252 |
WINDOWS-1252 | Windows西欧编码 |
ASCII |
US-ASCII | 美国信息交换标准码 |
EUC-JP |
日文编码 | |
SHIFT_JIS |
SJIS | 日文Shift-JIS编码 |
EUC-KR |
韩文编码 | |
KOI8-R |
俄文编码 |
# 查看所有支持的编码
iconv -l
# 查看支持的编码(过滤常用)
iconv -l | grep -E "(UTF|GB|BIG5|ISO|CP|ASCII)"
# 统计支持的编码数量
iconv -l | wc -l
# 查看特定编码的别名
iconv -l | grep -i gb2312
# 输出可能包含: GB2312 GB2312-80
# 创建测试文件(GBK编码)
echo "你好,世界!" | iconv -f UTF-8 -t GBK > test_gbk.txt
# 查看文件编码
file -i test_gbk.txt
# 输出: test_gbk.txt: text/plain; charset=iso-8859-1 (实际上是GBK)
# 从GBK转换到UTF-8(输出到屏幕)
iconv -f GBK -t UTF-8 test_gbk.txt
# 从GBK转换到UTF-8(保存到文件)
iconv -f GBK -t UTF-8 test_gbk.txt -o test_utf8.txt
# 验证转换结果
file -i test_utf8.txt
# 输出: test_utf8.txt: text/plain; charset=utf-8
# 从管道读取并转换
echo "Hello, 世界!" | iconv -f UTF-8 -t GBK
# 多个管道操作
cat gbk_file.txt | iconv -f GBK -t UTF-8 | grep "关键词"
# 与其他命令结合
iconv -f GBK -t UTF-8 input.txt | sed 's/旧/新/g' | iconv -f UTF-8 -t GBK > output.txt
# 转换为大写并转换编码
iconv -f GBK -t UTF-8 input.txt | tr 'a-z' 'A-Z' | iconv -f UTF-8 -t GBK
# Windows中文文件通常是GBK编码
# 转换GBK到UTF-8
iconv -f GBK -t UTF-8 windows_file.txt -o linux_file.txt
# 转换Windows换行符和编码
# 先转换编码,再处理换行符
iconv -f GBK -t UTF-8 win_file.txt | dos2unix > linux_file.txt
# 批量转换Windows文件
for file in *.txt; do
iconv -f GBK -t UTF-8 "$file" -o "${file%.txt}_utf8.txt"
done
# 下载网页并转换编码
curl -s http://example.com | iconv -f GB2312 -t UTF-8
# 处理HTML文件中的编码声明
# 先转换编码,再更新meta标签
iconv -f GBK -t UTF-8 webpage.html | \
sed 's/charset=gb2312/charset=utf-8/i' > webpage_utf8.html
# 批量转换HTML文件
find . -name "*.html" -exec sh -c '
for file; do
iconv -f GBK -t UTF-8 "$file" > "${file%.html}_utf8.html"
done
' sh {} +
# 转换SQL导出文件
iconv -f GBK -t UTF-8 db_dump_gbk.sql -o db_dump_utf8.sql
# 处理CSV文件
iconv -f GB2312 -t UTF-8 data.csv -o data_utf8.csv
# 转换JSON数据
iconv -f GBK -t UTF-8 data.json | jq '.' # jq需要UTF-8输入
# 创建包含无效字符的文件
echo -e "正常文本\x80无效字符" > test_bad.txt
# 默认情况下会报错
iconv -f UTF-8 -t GBK test_bad.txt
# 输出: iconv: 输入序列中第6个字符处有不完整的字符或移位序列
# 使用-c忽略无效字符
iconv -f UTF-8 -t GBK -c test_bad.txt
# 输出: 正常文本 (无效字符被忽略)
# 使用替代字符替换无效字符
echo -e "测试\x80字符" | iconv -f UTF-8 -t UTF-8//IGNORE
echo -e "测试\x80字符" | iconv -f UTF-8 -t UTF-8//TRANSLIT
# 1. 检测文件编码
file -i filename.txt
enca filename.txt # 需要安装enca
# 2. 查看原始字节
hexdump -C filename.txt | head -20
# 3. 尝试不同编码
for encoding in UTF-8 GBK GB2312 BIG5; do
echo "尝试编码: $encoding"
iconv -f $encoding -t UTF-8 filename.txt 2>/dev/null && echo "成功: $encoding"
done
# 4. 使用od查看字节
od -tx1 filename.txt | head -20
# 5. 创建测试用例
echo "测试文本" | iconv -f UTF-8 -t GBK > test_gbk.txt
iconv -f GBK -t UTF-8 test_gbk.txt
# iconv支持的特殊后缀:
# //IGNORE - 忽略无法转换的字符
# //TRANSLIT - 尝试音译无法转换的字符
# //ASCII - 转换为ASCII近似字符
# 示例:
# 忽略无效字符
iconv -f UTF-8 -t GBK//IGNORE file.txt
# 音译转换(如将ä转换为a)
echo "café naïve" | iconv -f UTF-8 -t ASCII//TRANSLIT
# 输出: cafe naive
# 结合使用
iconv -f UTF-8 -t ASCII//TRANSLIT//IGNORE file.txt
# 处理包含特殊符号的文本
echo "温度:25℃ 价格:€100" | iconv -f UTF-8 -t ASCII//TRANSLIT
# 输出: 温度:25? 价格:?100
#!/bin/bash
# batch_iconv.sh - 批量转换文件编码
SOURCE_ENCODING="${1:-GBK}"
TARGET_ENCODING="${2:-UTF-8}"
FILE_PATTERN="${3:-*.txt}"
echo "批量转换编码: $SOURCE_ENCODING → $TARGET_ENCODING"
echo "文件模式: $FILE_PATTERN"
echo "======================================"
# 创建备份目录
BACKUP_DIR="backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
# 计数器
converted=0
failed=0
# 处理文件
find . -maxdepth 1 -name "$FILE_PATTERN" -type f | while read file; do
echo "处理: $file"
# 备份原文件
cp "$file" "$BACKUP_DIR/"
# 尝试转换
if iconv -f "$SOURCE_ENCODING" -t "$TARGET_ENCODING" "$file" > "${file}.tmp" 2>/dev/null; then
# 检查转换是否真正改变了内容
if ! cmp -s "$file" "${file}.tmp"; then
mv "${file}.tmp" "$file"
echo " ✓ 转换成功"
((converted++))
else
rm "${file}.tmp"
echo " ⓘ 文件已经是目标编码"
fi
else
rm -f "${file}.tmp"
echo " ✗ 转换失败"
((failed++))
fi
done
echo ""
echo "转换完成!"
echo "成功: $converted 个文件"
echo "失败: $failed 个文件"
echo "备份保存在: $BACKUP_DIR/"
#!/bin/bash
# detect_and_convert.sh - 智能检测并转换编码
TARGET_ENCODING="UTF-8"
COMMON_ENCODINGS="UTF-8 GBK GB2312 BIG5 ISO-8859-1 CP1252"
for file in "$@"; do
if [ ! -f "$file" ]; then
echo "错误: 文件不存在 - $file"
continue
fi
echo "处理: $file"
# 使用file命令检测编码
detected=$(file -b --mime-encoding "$file")
echo " 检测到编码: $detected"
# 如果已经是目标编码,跳过
if [[ "$detected" == *"$TARGET_ENCODING"* ]] || [[ "$detected" == *"ascii"* ]]; then
echo " ✓ 已经是 $TARGET_ENCODING 编码,跳过"
continue
fi
# 尝试常见编码
converted=false
for encoding in $COMMON_ENCODINGS; do
if [ "$encoding" = "$TARGET_ENCODING" ]; then
continue
fi
if iconv -f "$encoding" -t "$TARGET_ENCODING" "$file" > "${file}.tmp" 2>/dev/null; then
if [ -s "${file}.tmp" ]; then
mv "${file}.tmp" "$file"
echo " ✓ 从 $encoding 转换为 $TARGET_ENCODING"
converted=true
break
fi
fi
rm -f "${file}.tmp"
done
if [ "$converted" = false ]; then
echo " ✗ 无法自动检测编码,请手动指定"
fi
done
# 转换Windows中文文档
iconv -f GBK -t UTF-8 document.doc.txt
# 处理混合编码的文件
# 先用grep分离不同编码部分
grep -a -P '[\x80-\xFF]' mixed.txt | iconv -f GBK -t UTF-8
grep -a -v -P '[\x80-\xFF]' mixed.txt # ASCII部分
# 批量处理office文档导出的文本
find . -name "*.txt" -exec sh -c '
if file -i "$1" | grep -q "gbk\|gb2312"; then
iconv -f GBK -t UTF-8 "$1" > "$1.utf8"
fi
' sh {} \;
# 转换中文日志文件
iconv -f GB2312 -t UTF-8 /var/log/chinese.log
# 实时监控并转换日志
tail -f app.log | iconv -f GBK -t UTF-8
# 批量转换日志文件
for logfile in /var/log/*.log; do
if file -i "$logfile" | grep -q gb; then
iconv -f GBK -t UTF-8 "$logfile" > "${logfile}.utf8"
fi
done
# 迁移旧系统数据到新系统
# 旧系统GBK,新系统UTF-8
mysqldump -u olduser -p olddb | \
iconv -f GBK -t UTF-8 | \
mysql -u newuser -p newdb
# 转换CSV文件用于新系统
iconv -f GB2312 -t UTF-8 old_data.csv > new_data.csv
# 批量转换数据文件
find /data/old -type f -name "*.dat" -exec \
iconv -f GBK -t UTF-8 {} -o /data/new/{} \;
| 工具 | 特点 | 优点 | 缺点 |
|---|---|---|---|
iconv |
标准编码转换工具 | 支持编码多,标准工具 | 需要指定源编码 |
recode |
另一种编码转换工具 | 支持更多格式转换 | 安装率较低 |
enca |
自动编码检测和转换 | 自动检测编码 | 检测不一定准确 |
uconv |
ICU转换工具 | 支持最新Unicode标准 | 复杂,学习成本高 |
python |
编程语言处理 | 灵活,可编程 | 需要Python环境 |
vim |
编辑器内转换 | 可视化编辑 | 批量处理不便 |
# 使用enca检测编码,然后用iconv转换
encoding=$(enca -L zh_CN file.txt | awk '{print $2}')
iconv -f "$encoding" -t UTF-8 file.txt
# 结合file命令
encoding=$(file -b --mime-encoding file.txt | cut -d= -f2)
iconv -f "$encoding" -t UTF-8 file.txt
# 使用vim转换(交互式)
vim file.txt
# 在vim中:set fileencoding=utf-8 然后 :w
# 使用Python转换
python3 -c "import sys; print(open(sys.argv[1], encoding='gbk').read())" file.txt
# 查看文件是否包含BOM
head -c 3 file.txt | od -x
# 如果显示 ef bb bf 则是UTF-8 BOM
# 移除UTF-8 BOM
sed -i '1s/^\xef\xbb\xbf//' file_with_bom.txt
# 添加UTF-8 BOM
printf '\xef\xbb\xbf' | cat - file.txt > file_with_bom.txt
# 转换时处理BOM
# 有些系统需要特殊处理
iconv -f UTF-8 -t GBK file.txt | sed '1s/^\xef\xbb\xbf//'
# 使用专门的BOM工具
# bom工具需要安装
bom -s file.txt # 剥离BOM
bom -a file.txt # 添加BOM
# 1. 批量处理时使用find + xargs
find . -name "*.txt" -print0 | xargs -0 -I {} iconv -f GBK -t UTF-8 {} -o {}.utf8
# 2. 使用parallel并行处理
# 需要安装GNU parallel
find . -name "*.txt" | parallel iconv -f GBK -t UTF-8 {} -o {}.utf8
# 3. 先检测是否需要转换(避免重复转换)
for file in *.txt; do
if ! file -i "$file" | grep -q utf-8; then
iconv -f GBK -t UTF-8 "$file" -o "${file}.utf8"
fi
done
# 4. 使用内存映射(对于超大文件)
# iconv本身是流式处理,但可以配合其他工具
dd if=largefile.txt bs=1M | iconv -f GBK -t UTF-8 | dd of=converted.txt bs=1M
# 5. 避免不必要的中间文件
# 使用管道而不是临时文件
iconv -f GBK -t UTF-8 input.txt | grep "关键词" > output.txt
这个错误通常是因为指定的源编码不正确。解决方法:
# 1. 先检测文件实际编码
file -i filename.txt
enca filename.txt # 如果安装了enca
# 2. 尝试常见编码
for enc in UTF-8 GBK GB2312 BIG5 ISO-8859-1; do
echo "尝试编码: $enc"
iconv -f $enc -t UTF-8 filename.txt 2>/dev/null && break
done
# 3. 使用-c选项忽略错误(可能丢失数据)
iconv -f GBK -t UTF-8 -c filename.txt
# 4. 使用特殊后缀
iconv -f GBK//IGNORE -t UTF-8 filename.txt
# 5. 检查文件是否损坏
# 查看文件开头是否有异常字节
head -c 100 filename.txt | od -x
# 6. 尝试其他转换工具
recode GBK..UTF8 filename.txt
uconv -f GBK -t UTF-8 filename.txt
# 方法1:使用file命令(系统自带)
file -i filename.txt
# 输出: filename.txt: text/plain; charset=iso-8859-1
file -b --mime-encoding filename.txt
# 输出: iso-8859-1
# 方法2:安装并使用enca(更准确)
# Ubuntu/Debian
sudo apt-get install enca
# CentOS/RHEL
sudo yum install enca
# 使用enca检测
enca -L zh_CN filename.txt # 指定语言为中文
enca filename.txt # 自动检测语言
# 方法3:使用chardet(Python库)
# 安装
pip install chardet
# 使用
chardet filename.txt
python3 -c "import chardet; print(chardet.detect(open('filename.txt', 'rb').read()))"
# 方法4:组合使用
detect_encoding() {
local file="$1"
# 先尝试enca
if command -v enca &> /dev/null; then
enca -L zh_CN "$file" 2>/dev/null | awk '{print $2}'
return
fi
# 再尝试file
file -b --mime-encoding "$file" | sed 's/.*=//'
}
# 方法5:编写检测脚本
#!/bin/bash
for encoding in UTF-8 GBK GB2312 BIG5; do
if iconv -f "$encoding" -t UTF-8 "$1" > /dev/null 2>&1; then
echo "$encoding"
exit 0
fi
done
echo "unknown"
转换后出现乱码的常见原因和解决方法:
# 1. 源编码指定错误(最常见)
# 尝试不同的源编码
for enc in GBK GB2312 GB18030 BIG5; do
echo "=== 尝试 $enc ==="
iconv -f $enc -t UTF-8 file.txt | head -5
done
# 2. 目标编码不支持某些字符
# 检查目标编码是否支持所需字符
iconv -l | grep -i "目标编码"
# 3. 文件本身已经损坏
# 查看文件的原始字节
hexdump -C file.txt | head -20
# 4. 终端显示问题(文件实际正确)
# 检查文件实际内容
od -c file.txt | head -20
# 或在其他终端/编辑器中打开
# 5. BOM问题
# 检查文件开头是否有BOM
head -c 3 file.txt | od -x
# 移除BOM
sed -i '1s/^\xef\xbb\xbf//' file.txt
# 6. 使用正确的查看工具
# 确保查看工具使用正确的编码
# 在vim中检查
vim file.txt
# 输入:set fileencoding
# 7. 使用十六进制编辑器查看
hexedit file.txt # 需要安装hexedit
xxd file.txt | head -20
# 8. 创建测试用例验证
echo "测试文本" | iconv -f UTF-8 -t GBK > test_gbk.txt
iconv -f GBK -t UTF-8 test_gbk.txt
# 如果这个测试正常,说明iconv工作正常
# 方法1:使用find + exec
find /path/to/dir -type f -name "*.txt" -exec sh -c '
for file; do
iconv -f GBK -t UTF-8 "$file" > "${file}.utf8"
done
' sh {} +
# 方法2:使用find + xargs(更高效)
find /path/to/dir -type f -name "*.txt" -print0 | \
xargs -0 -I {} iconv -f GBK -t UTF-8 {} -o {}.utf8
# 方法3:递归处理所有文本文件
find /path/to/dir -type f -exec file {} \; | \
grep -i "text" | cut -d: -f1 | \
while read file; do
iconv -f GBK -t UTF-8 "$file" -o "${file}.utf8"
done
# 方法4:使用parallel并行处理
find /path/to/dir -type f -name "*.txt" | \
parallel iconv -f GBK -t UTF-8 {} -o {}.utf8
# 方法5:保留目录结构
find /path/to/dir -type f -name "*.txt" | while read file; do
# 创建对应的输出目录
out_dir="/output/path/$(dirname "${file#/path/to/dir/}")"
mkdir -p "$out_dir"
# 转换文件
iconv -f GBK -t UTF-8 "$file" -o "$out_dir/$(basename "$file")"
done
# 方法6:使用bash脚本(更灵活)
#!/bin/bash
convert_dir() {
local src_dir="$1"
local dst_dir="$2"
local from_enc="${3:-GBK}"
local to_enc="${4:-UTF-8}"
find "$src_dir" -type f | while read src_file; do
# 计算相对路径
rel_path="${src_file#$src_dir/}"
dst_file="$dst_dir/$rel_path"
# 创建目标目录
mkdir -p "$(dirname "$dst_file")"
# 转换文件
if file -b --mime-encoding "$src_file" | grep -qi "text"; then
iconv -f "$from_enc" -t "$to_enc" "$src_file" -o "$dst_file"
else
# 非文本文件直接复制
cp "$src_file" "$dst_file"
fi
done
}
file文件类型检测
enca编码检测工具
recode另一种转换工具
uconvICU转换工具
# 使用file命令快速检测
alias charenc='file -b --mime-encoding'
# 检测多个文件
for f in *.txt; do
echo "$f: $(charenc "$f")"
done
# 检测并统计
file -b --mime-encoding *.txt | sort | uniq -c
# 创建UTF-8测试文件
echo "UTF-8测试: 你好,世界!" > test_utf8.txt
# 创建GBK测试文件
echo "GBK测试: 你好,世界!" | iconv -f UTF-8 -t GBK > test_gbk.txt
# 创建混合编码测试
echo -e "ASCII部分\n\xe4\xbd\xa0\xe5\xa5\xbd GBK部分" > test_mixed.txt
# 验证转换
iconv -f GBK -t UTF-8 test_gbk.txt
iconv -f UTF-8 -t GBK test_utf8.txt | iconv -f GBK -t UTF-8
创建一个完整的编码处理工具包,包含以下功能:自动检测文件编码、智能转换为目标编码(默认UTF-8)、处理BOM标记、批量处理目录、生成转换报告、支持配置文件,并提供命令行和图形界面两种使用方式。