一文搞清UNIX/Linux与Windows文件换行符格式差异

简介: 一文搞清UNIX/Linux与Windows文件换行符格式差异
  • 当一个文件在Windows和Linux上交替操作后,经常遇到一些莫名其妙的问题,如shell脚本无法执行,找不到shell脚本等问题,本文j谨就这一问题做一总结,供各位参考;



格式差异


  • 换行符是行尾 (EOL),是一个特殊的字符或字符序列,表示一行文本的结尾和新行的开头;


  • 表示换行符的实际代码因操作系统而异:


– Microsoft Windows,DOS(MS-DOS,PC DOS等)使用CR + LF;


– Unix和类Unix系统使用,包括Linux,OS X,FreeBSD等使用LF;


– MAC系统里,使用CR;


  • CR即ASCII码的0x0D(\r),LF为ASCII码的0x0A(\n),DOS下使用(\r\n),类Unix系统下使用(\n),DOS系统下的’\r’在类Unix系统下会被显示为 ^M。


后文仅以test-dos.sh文件为例来说明,具体内容如下:


#!/bin/bash
echo "Hello World !"


格式影响


直观影响


  • Unix/Mac系统下的文件在Windows里打开的话,所有文字会变成一行(由于Windows下编辑器的处理,这种情况一般不会发生);


  • 而Windows里的文件在Unix/Mac下打开的话,在每行的结尾可能会多出一个^M符号;


功能影响


  • 在windows上编写的shell、python等脚本在Linux上⽆法正常的执⾏,会有 ^M 相关提⽰:


[qxhgd@localhost crlf]$ ./test-dos.sh
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory


  • 如果在make编译的时候,执行mksh(一个shell文件)可能会有类似下面的提示:


make[3]: ./mksh: Command not found


格式查看


Windows下查看


  • 利用编辑器,如Visual Studio Code、UltraEdit、Notepad2等软件,如在状态栏显示为CR+LF则为Windows格式,如果显示为LF则为Linux格式:




  • 利用支持扩展搜索的编辑器,如Notepad++,查找\r\n:



Linux下查看


  • cat命令


显示^M:


[qxhgd@localhost crlf]$ cat -v test-dos.sh
#!/bin/bash^M
echo "Hello World !"^M


显示Tab:


[qxhgd@localhost crlf]$ cat -T test-dos.sh
#!/bin/bash
^Iecho "Hello World !"


  • od命令


od可以单独使用:


[qxhgd@localhost crlf]$ od -c test-dos.sh
0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n   e   c   h
0000020   o       "   H   e   l   l   o       W   o   r   l   d       !
0000040   "
0000041


也可以和cat配合使用:


cat test-dos.sh| od -c


  • hexdump命令


[qxhgd@localhost crlf]$ hexdump -c test-dos.sh
0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n   e   c   h
0000010   o       "   H   e   l   l   o       W   o   r   l   d       !
0000020   "
0000021


  • vim


状态栏下会显示:


“test-dos.sh” [noeol][dos] 2L, 33B


命令模式下执行set ff:


fileformat=dos


  • gedit


– 首先使用gedit打开文件:


[qxhgd@localhost crlf]$ gedit test-dos.sh


– 搜索\r\n,如果搜索到了就表示是DOS格式:



格式修改


Windows下


  • 可以利用编辑器修改,如Visual Studio Code,点击状态栏右下方的CRLF,选择“行尾序列”可修改为LF的格式;


  • 有的编辑器,如Notepad2,有Line Endings可供选择:



  • 利用支持扩展搜索的编辑器,如Notepad++,可将\r\n替换掉:



Linux下


利用特殊工具转换


  • vim


vim命令模式下,执行set ff=unix或set fileformat=unix即可将DOS格式转换为unix格式;


  • dos2unix


需要额外用命令安装,一般的Linux系统不带的;unix2dos与dos2unix作用正相反。


[qxhgd@localhost crlf]$ dos2unix test-dos.sh
dos2unix: converting file test-dos.sh to Unix format ...
[qxhgd@localhost crlf]$ dos2unix -n test-dos.sh test-unix.sh
dos2unix: converting file test-dos.sh to file test-unix.sh in Unix format ...


  • tofrodos


这一组一共两个命令,todos和fromdos,fromdos用来将dos转换成unix格式,todos是用于将unix转换成dos格式的,使用例子如下:


[qxhgd@localhost crlf]$ fromdos test-dos.sh


利用文本处理工具


  • sed


– 转换一个文件:


sed ‘s/^M//’ test-dos.sh> test-unix.sh


– 转换多个文件:


find ./ -type f print0 | xargs -0 sed -i 's/^M$//'


  • vi


– 1、vi test-dos.sh


– 2、:%s/^M//g或:%s/\r//g


– 3、esc退出 :wq保存退出


其中^M 必须是同时按 Ctrl+V+M(按住Ctrl键,然后依次V、M键)或依次按Ctrl + V然后Ctrl + M,表示回车。


  • tr


tr -d "\015" test-dos.sh                  
cat test-dos.sh|tr -d ‘/r' > test-unix.sh 
tr -d '\r' < test-dos.sh > test-unix.sh


  • perl


cat test-dos.sh | perl -pe ‘~s/\r//g’ > test-unix.sh
perl -p -e 's/\r//g' test-dos.sh> test-unix.sh
perl -pi -e 's/\r\n/\n/g' test-dos.sh
相关文章
|
18天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
开发框架 数据安全/隐私保护 开发者
HBuilder开发者必备!Windows上传IPA文件的软件分享
HBuilder开发者必备!Windows上传IPA文件的软件分享
21 1
|
7天前
|
存储 安全 文件存储
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
|
11天前
|
SQL 监控 安全
Linux&Windows 日志分析 陇剑杯 CTF
Linux&Windows 日志分析 陇剑杯 CTF
|
14天前
|
Linux Windows
Windows、Mac、Linux解决端口被占用的问题
Windows、Mac、Linux解决端口被占用的问题
23 1
|
14天前
|
Windows
【Windows】 手写脚本更快编辑hosts文件
【Windows】 手写脚本更快编辑hosts文件
15 0
|
21天前
|
安全 Ubuntu Linux
Linux远程访问Windows实现步骤
在Windows上启用远程桌面连接并获取IP地址后,Linux用户需安装SSH客户端( Debian系:`sudo apt-get update; sudo apt-get install openssh-client`,RPM系:`sudo yum install openssh-clients`)。然后使用命令`ssh 用户名@Windows_IP地址`连接,其中`用户名`和`Windows_IP地址`按实际情况填写。
15 4
|
1月前
|
存储 算法 Linux
【Linux 系统标准 进程资源】Linux 创建一个最基本的进程所需的资源分析,以及线程资源与之的差异
【Linux 系统标准 进程资源】Linux 创建一个最基本的进程所需的资源分析,以及线程资源与之的差异
25 0
|
1月前
|
Linux 数据安全/隐私保护 Docker
linux和windows中安装emqx消息服务器
linux和windows中安装emqx消息服务器
41 0