网站建设中,中国大陆地区请使用VPN访问,欢迎提建议,关注LSKR Mastodon

windows与Linux互传文件编码转换和换行符处理

本文描述了在windows和linux之间互换文件,在linux上需要做的换行符转换dos2unix和文件编码转换iconv操作。

编码转换

将windows文件转换成支持Linux文件的换行符号,使用Unix2dos,那么如何装换文件编码呢?

举例子

有一个文件使用Big5 编码,如何装换成UTF-8 编码呢?

iconv

 sboxes@osboxes ~/D/vmware-tools-distrib> iconv --help
 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:
 <https://bugs.launchpad.net/ubuntu/+source/eglibc/+bugs>

常用语法

 iconv -f [原来编码] -t [新编码] file -o newfile

查看字符编码

 file -i filename

换行符转换

当把DOS文件在Linux 上打开的时候,比如:使用命令 cat -A file 我们会发现DOS换行符 ^M$ ,也被称为CRLF两个符号。但是在Linux 上仅有LF一个符号表示换行符。

转换

Linux系统中,提供了命令支持两者之间的转换

 dos2unix -kn file newfile 
 unix2dos -kn file newfile
 #含义:保留修改时间,保留源文件,转换成newfile

这里的unix就是Linux的前身,所以这里是一脉想成的,Linux发展都深受到unixminix 的影响,所以有大量相似之处。

使用

在不同系统 Windows 和 Linux 上复制纯文本,就需要使用unix2dos 或者dos2unix 进行转换。不建议在 Windows 上编辑好了文件上传到 Linux上,会容易发生错误。

发表评论

Cookie Consent
我们使用 Cookie 来了解您如何使用我们的网站并提升您的体验。这包括个性化内容和广告。
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.