linux下改变文件的字符编码

首先确定文件的原始字符编码:

$ file -bi test.txt

然后用 iconv 转换字符编码

$ iconv -f from-encoding -t to-encoding file > new-file

如果上面的步骤更改不成功,可以使用 vim 来更改文件的字符编码

先打开文件,然后设置文件的字符编码,在命令模式使用

set encoding=utf-8
set fileencoding=utf-8              (会改变正在编辑的文件的字符编码,千万别保存,一定要另存为, vim 的另存格式为 :w  new-file)

对于windows的编码,很多时候现实iso-8859,其实转码的时候应该使用 cp936:

iconv -f cp936 file_name -t UTF-8 -o file_name

对于多个文件,可以使用下面的命令来批量转:

find . -iname "*.h" | xargs -i sh -c "iconv -f cp936 {} -t UTF-8 -o {}"

查看有哪些文件是iso编码:

find . -iname "*" | xargs file | ack -i iso
原文地址:https://www.cnblogs.com/welhzh/p/3555122.html