Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1658537
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类: LINUX

2014-06-09 13:04:58

原文地址:Linux命令之dos2unix 作者:centrify

Linux命令之dos2unix - 将DOS格式文本文件转换成UNIX格式

用途说明

dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的(DOS/MAC to UNIX text file format converter)。DOS下的文本文件是以\r\n作为断行标志的,表示成十六进制就是0D 0A。而Unix下的文本文件是以\n作为断行标志的,表示成十六进制就是 0A。DOS格式的文本文件在Linux底下,用较低版本的vi打开时行尾会显示^M,而且很多命令都无法很好的处理这种格式的文件,如果是个shell脚本,。而Unix格式的文本文件在Windows下用Notepad打开时会拼在一起显示。因此产生了两种格式文件相互转换的需求,对应的将UNIX格式文本文件转成成DOS格式的是unix2dos命令。
常用参数
将DOS格式文本文件转换成Unix格式,最简单的用法就是dos2unix直接跟上文件名。
格式:dos2unix file
如果一次转换多个文件,把这些文件名直接跟在dos2unix之后。(注:也可以加上-o参数,也可以不加,效果一样)
格式:dos2unix file1 file2 file3
格式:dos2unix -o file1 file2 file3
上面在转换时,都会直接在原来的文件上修改,如果想把转换的结果保存在别的文件,而源文件不变,则可以使用-n参数。
格式:dos2unix oldfile newfile
如果要保持文件时间戳不变,加上-k参数。所以上面几条命令都是可以加上-k参数来保持文件时间戳的。
格式:dos2unix -k file
格式:dos2unix -k file1 file2 file3
格式:dos2unix -k -o file1 file2 file3
格式:dos2unix -k -n oldfile newfile
注:unix2dos命令的使用方式与dos2unix命令的类似。
使用示例


示例一 DOS格式文本文件在Linux下的表现, 现在有一个脚本文件test.sh,是在Linux下用vi编辑的

test.sh

  1. [root@sobey wesoft]# cat test.sh
  2. #!/bin/bash
  3. usage () {
  4. cat <<-END_USAGE
  5. dsd
  6. dsdsd
  7. dsd
  8. END_USAGE
  9. }
  10. usage
  11. [root@sobey wesoft]#

现在把它转换成DOS格式文本文件。)折叠或打开

  1. [root@sobey wesoft]# unix2dos test.sh
  2. unix2dos: converting file test.sh to DOS format ...

  3. [root@sobey wesoft]# ./test.sh
  4. -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

  5. [root@sobey wesoft]# cat test.sh
  6. #!/bin/bash
  7. usage () {
  8. cat <<-END_USAGE
  9. dsd
  10. dsdsd
  11. dsd
  12. END_USAGE
  13. }
  14. usage

DOS格式的脚本文件时无法解释执行的,因为脚本文件的第一行是用来指定解释器的,Linux系统认为解释器是/bin/sh^M,而不是/bin/sh。


我们来通过Linux下的一些命令来看一下DOS格式文件的真面目

  1. [root@sobey wesoft]# cat -v test.sh
  2. #!/bin/bash^M
  3. usage () {^M
  4. cat <<-END_USAGE^M
  5. dsd^M
  6. dsdsd^M
  7. dsd^M
  8. END_USAGE^M
  9. }^M
  10. usage^M


hexdump -C可以看到文件每个字节的十六进制表示。)折叠或打开

  1. [root@sobey wesoft]# hexdump -C test.sh
  2. 00000000 23 21 2f 62 69 6e 2f 62 61 73 68 0d 0a 75 73 61 |#!/bin/bash..usa|
  3. 00000010 67 65 20 28 29 20 7b 0d 0a 63 61 74 20 3c 3c 2d |ge () {..cat <<-|
  4. 00000020 45 4e 44 5f 55 53 41 47 45 0d 0a 64 73 64 0d 0a |END_USAGE..dsd..|
  5. 00000030 64 73 64 73 64 0d 0a 64 73 64 0d 0a 45 4e 44 5f |dsdsd..dsd..END_|
  6. 00000040 55 53 41 47 45 0d 0a 7d 0d 0a 75 73 61 67 65 0d |USAGE..}..usage.|
  7. 00000050 0a |.|
  8. 00000051

  9. [root@sobey wesoft]#

现在我们把DOS格式改回Unix格式的,看看效果。)折叠或打开

  1. [root@sobey wesoft]# dos2unix test.sh
  2. dos2unix: converting file test.sh to UNIX format ...
  3. [root@sobey wesoft]# ./test.sh
  4. dsd
  5. dsdsd
  6. dsd

示例二 dos2unix -k和dos2unix -n的使用示例


  1. [root@sobey wesoft]# cat <<EOF >1.txt
  2. > 1
  3. > 2

  4. > 3
  5. > EOF

  6. [root@sobey wesoft]# file 1.txt
  7. 1.txt: ASCII text

  8. [root@sobey wesoft]# ls -l 1.txt
  9. -rw-r--r-- 1 root root 6 11-14 09:08 1.txt

  10. [root@sobey wesoft]# date
  11. 2010年 11月 14日 星期日 09:28:42 CST

  12. [root@jfht ~]# unix2dos -k 1.txt <== 保持文件时间戳
  13. unix2dos: converting file 1.txt to DOS format ...

  14. [root@sobey wesoft]# ls -l 1.txt
  15. -rw-r--r-- 1 root root 9 11-14 09:08 1.txt

  16. [root@jfht ~]# dos2unix -n 1.txt 2.txt <== 将1.txt转换到2.txt
  17. dos2unix: converting file 1.txt to file 2.txt in UNIX format ...

  18. [root@sobey wesoft]# ls -l 1.txt 2.txt
  19. -rw-r--r-- 1 root root 9 11-14 09:08 1.txt
  20. -rw-r--r-- 1 root root 6 11-14 09:30 2.txt

  21. [root@sobey wesoft]# file 1.txt 2.txt
  22. 1.txt: ASCII text, with CRLF line terminators
  23. 2.txt: ASCII text

  24. [root@sobey wesoft]# cat -v 1.txt
  25. 1^M
  26. 2^M
  27. 3^M

  28. [root@sobey wesoft]# cat -v 2.txt
  29. 1
  30. 2
  31. 3
  32. [root@sobey wesoft]#


阅读(734) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~