一文搞清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
相关文章
|
3月前
|
安全 Linux 生物认证
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
154 0
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
|
3月前
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
449 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
3月前
|
安全 Linux 生物认证
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
228 17
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
|
3月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
204 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
3月前
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
298 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
3月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
242 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
734 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
4月前
|
JSON 安全 Linux
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
187 1
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
|
3月前
|
Linux iOS开发 计算机视觉
GIMP 3.0.6 (Linux, macOS, Windows) 发布 - 免费开源图像编辑器
GIMP 3.0.6 (Linux, macOS, Windows) 发布 - 免费开源图像编辑器
317 0
|
4月前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
157 0

热门文章

最新文章