Welcome 微信登录

首页 / 操作系统 / Linux / Linux文本文件与WIN文本文件换行格式转换命令

有时在WIN下编辑好的脚本文件上传到LINUX服务器中不能正常执行,开始误认为是LINUX配置问题,后来发现,是WIN与LINUX存储文件时的换行符标志不同造成的。在DOS使用的换行符为 ^M$,我们称为CR与LF两个符号。而在Linux中,则仅有LF ($) 这个换行符。可以用如下命令完成格式转换:$dos2unix,$unix2dos。但这两个命令在Ubuntu发行版本中不存在,可通过:$sudo apt-get install tofrodos命令安装。之后,再次使用如下文所示的格式即可。[root@linux ~]# dos2unix [-kn] file [newfile][root@linux ~]# unix2dos [-kn] file [newfile]参数:-k : 保留该文件原来的mtime时间格式(不更新文件上次内容经过修改的时间)-n : 保留原来的旧文件,将转换后的内容输出到新文件,如:dos2unix -n old new范例:范例一:将提供的hosts文件格式更新为dos格式。[root@linux ~]# unix2dos -k hostsunix2dos: converting file hosts to DOS format ...# 此时hosts文件的时间不会改变,但内容主要将换行符修改成为DOS的CRLF了。范例二:将范例一已经变成DOS格式的hosts改名为hosts.dos,并且转换Linux        格式到hosts.linux[root@linux ~]# mv hosts hosts.dos[root@linux ~]# dos2unix -k -n hosts.dos hosts.linuxdos2unix: converting file hosts.dos to file hosts.linux in UNIX format ...[root@linux ~]# ll-rw-r--r-- 1 root root       288 Aug 1 13:30 hosts.dos-rw------- 1 root root      279 Aug 1 13:30 hosts.linux# 由于DOS格式中多了CR字符,所以,文件比较大。Linux bonding 之balance-alb 原理介绍及其实现Linux帐号管理的passwd和shadow文件相关资讯      Linux教程 
  • Linux教程:如何在命令行中查看目  (07/28/2014 12:22:23)
  • Linux 修改root密码  (11/03/2012 07:53:38)
  • su - root 与su root的区别  (06/06/2012 00:39:40)
  • Linux进程间通信:消息队列  (01/28/2013 09:43:00)
  • U盘安装Linux开机无法启动解决方法  (10/07/2012 08:55:52)
  • Windows 7/Linux 同步时间  (05/15/2012 06:17:55)
本文评论 查看全部评论 (0)
表情: 姓名: 字数