`
houchengvip
  • 浏览: 29844 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Linux查看文件编码格式及文件编码转换

阅读更多

Linux查看文件编码格式及文件编码转换

如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题。Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8。下面介绍一下,在Linux中如何查看文件的编码及如何进行对文件进行编码转换。

查看文件编码
在Linux中查看文件编码可以通过以下几种方式:
1.在Vim中可以直接查看文件编码
:set fileencoding
即可显示文件编码格式。
如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可以在
~/.vimrc 文件中添加以下内容:

set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

这样,就可以让vim自动识别文件编码(可以自动识别UTF-8或者GBK编码的文件),其实就是依照
fileencodings提供的编码列表尝试,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。
2. enca (如果你的系统中没有安装这个命令,可以用sudo yum install -y enca 安装 )查看文件编码
$ enca filename
filename: Universal transformation format 8 bits; UTF-8
CRLF line terminators
需要说明一点的是,enca对某些GBK编码的文件识别的不是很好,识别时会出现:
Unrecognized encoding

文件编码转换
1.在Vim中直接进行转换文件编码,比如将一个文件转换成utf-8格式
:set fileencoding=utf-8

2. enconv 转换文件编码,比如要将一个GBK编码的文件转换成UTF-8编码,操作如下
enconv -L zh_CN -x UTF-8 filename

3. iconv 转换,iconv的命令格式如下:
iconv -f encoding -t encoding inputfile
比如将一个UTF-8 编码的文件转换成GBK编码
iconv -f GBK -t UTF-8 file1 -o file2

如果你只是想对文件名进行编码转换,可以参照这篇文章:
Linux文件名编码转换
分享到:
评论
2 楼 rem1x 2011-06-29  
xserver 写道
误人子弟~


SYNOPSIS
       iconv -f encoding -t encoding inputfile

DESCRIPTION
       The iconv program converts the encoding of characters in inputfile from one coded character set to
       another. The result is written to standard output unless otherwise specified by the --output
       option.

       --from-code, -f encoding

       Convert characters from encoding

       --to-code, -t encoding

那是你系统上的iconv版本太老了

Usage: iconv [OPTION...] [FILE...]
Convert encoding of given files from one encoding to another.

Input/Output format specification:
  -f, --from-code=NAME       encoding of original text
  -t, --to-code=NAME         encoding for output

Information:
  -l, --list                 list all known coded character sets

Output control:
  -c                         omit invalid characters from output
  -o, --output=FILE          output file
  -s, --silent               suppress warnings
      --verbose              print progress information

  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
1 楼 xserver 2011-02-12  
误人子弟~


SYNOPSIS
       iconv -f encoding -t encoding inputfile

DESCRIPTION
       The iconv program converts the encoding of characters in inputfile from one coded character set to
       another. The result is written to standard output unless otherwise specified by the --output
       option.

       --from-code, -f encoding

       Convert characters from encoding

       --to-code, -t encoding

相关推荐

Global site tag (gtag.js) - Google Analytics