iconv:文本文件编码转换
用 iconv 在 Linux 上把 GBK、Latin-1 等编码文件转换为 UTF-8,修复乱码。
执行 iconv -f 源编码 -t UTF-8 文件 -o 输出文件 即可转码(-l 可列出全部支持的编码)。
01命令
命令
$ iconv -f GBK -t UTF-8 input.txt -o output.txt02作用说明
iconv 把文件的字节从一种字符集转换到另一种——是“在某个编辑器里打开全是乱码、换个编辑器又正常”这类文件的标准修复手段。常见场景包括 GB2312/GBK 转 UTF-8、清理 Windows-1252/Latin-1、处理 Windows 工具导出的 UTF-16 文件。默认结果写到 stdout,所以要用 -o(或重定向)命名输出文件;目标字符集缺字时可用 //TRANSLIT 近似转换。
03风险
!注意: 输入输出同名做原地转换时,一旦源编码判断错误文件就会被毁——务必先写到新文件。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- iconv(1) — Arch Linux manual依据: -f source encoding, -t target encoding, -o output file, -l lists supported encodings
- iconv(1) — Ubuntu manpage依据: Documents iconv conversion between encodings with --from-code and --to-code
最后验证: 2026-09-17 · 分类: 文件与文件夹