linux下获取文件的编码和改变文件的编码

linux下获取文件的编码可以使用命令 file , 或者 enca ,通过vim编辑的时候也可以查看。下面给出一些示例...

查看文件的编码

ascii 示例

echo "test" > test.txt
file test.txt

#输出的结果
test.txt: ASCII text

enca -L zh_CN test.txt

# 输出的结果
7bit ASCII characters

汉字测试

echo "今天" > test.txt
file test.txt

# 输出
test.txt: UTF-8 Unicode text
enca -L zh_CN test.txt

# 输出
Universal transformation format 8 bits; UTF-8

修改文件编码

使用 enca 修改文件的编码, enca比较智能可以自动判断当前的文件编码,所以不用给出参数说明当前的文件编码格式。
enca 使用的时候需要指定语言 。比如 -L zh_CN 指定简体中文。

# 把utf8编码修改成 gbk
enca -L zh_CN -x gbk test.txt
enca -L zh_CN test.txt
Simplified Chinese National Standard; GB2312

# 把gbk编码修改成utf8编码
enca -L zh_CN -x utf-8 test.txt

enca -L zh_CN test.txt
Universal transformation format 8 bits; UTF-8

enca 可以使用通配符比如

enca -L zh_CN -x utf-8 *.html

enca 不覆盖当前的文件

enca -L zh_CN -x UTF-8 < a.html > a.utf8.html

其他的方法
1. convmv
2. 在vim中使用 命令模式 :set fileencoding 查看文件编码, set fileencoding=utf-8 修改当前文件编码。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注