Linux command Dos2unix-convert DOS format text files to UNIX format usage notes

Source: Internet
Author: User
Tags printable characters


The Dos2unix command is used to convert a DOS-formatted text file into Unix (Dos/mac to UNIX text, format Converter). Dos text file is based on \ r \ n As a break mark, expressed as 16 binary is 0D 0 A. The text file under Unix is marked with \ n as a break mark, which means that the 16 binary is 0 a. The DOS format text file under Linux, with the lower version of VI Open the end of the ^m will be displayed, and many commands are not very good processing of this format of the file, if it is a shell script. The UNIX-formatted text files are displayed together when opened with Notepad under Windows. As a result, there is a need to convert the two format files to each other, and the Unix2dos command that translates the UNIX format text file into a DOS format.

Common parameters

The simplest way to convert a DOS-formatted text file into a UNIX format is to dos2unix the file name directly.

Format: Dos2unix file

If you convert more than one file at a time, follow these file names directly after Dos2unix. (Note: can also be added to the-o parameter, also can not add, the same effect)

Format: Dos2unix file1 file2 file3

Format: Dos2unix-o file1 file2 file3

The above in the conversion, will be directly on the original file modification, if you want to save the results of the conversion of other files, and the source file is not changed, you can use the-n parameter.

Format: Dos2unix oldfile newfile

If you want to keep the file timestamp constant, add the-K parameter. So the above commands can be added with the-K parameter to keep the file timestamp.

Format: Dos2unix-k file

Format: dos2unix-k file1 file2 file3

Format: dos2unix-k-o file1 file2 file3

Format: dos2unix-k-n oldfile newfile

Note: The Unix2dos command is used in a similar way to the Dos2unix command.

Using the sample Example a DOS-formatted text file behaves under Linux

Now there is a script file job.sh, which is edited with VI under Linux.

[Email protected] ~]# cat job.sh
#!/bin/sh

Date >job.txt

now convert it to a DOS-formatted text file.
[[email protected] ~]# 

Try to run a little bit.
[Email protected] ~]# ./job.sh
-bash:./job.sh: Insufficient permissions
[Email protected] ~]# chmod +x job.sh
[Email protected] ~]# ./job.sh
-bash:./job.sh:/bin/sh^m:bad Interpreter: No file or directory

DOS-formatted script file cannot be interpreted because the first line of the script file is used to specify the interpreter, and the Linux system considers the interpreter to be/bin/sh^m, not/bin/sh.

Let's take a look at some of the Linux commands to see the true face of the DOS format file.
[Email protected] ~]#cat-v job.sh<== Cat-v can see non-printable characters in the file, and the cat command without the-v parameter does not work.
#!/bin/sh^m
^m
Date >job.txt^m
^m
[Email protected] ~]#hexdump-c job.sh<== Hexdump-c can see the hexadecimal representation of each byte of the file.
00000000 2f 6e 2f 73 680d 0a0d 0a|#!/bin/sh....dat|
00000010 3e 6a 6f, 2e 74 78 740d 0a 0d 0a|e >job.txt....|
0000001e
[Email protected] ~]#VI job.sh<== use VI to open when you can see the "DOS" at the bottom of the format prompt. Some version vi shows the end of the line as ^m.

#!/bin/sh

Date >job.txt

~
~

"Job.sh" [dos ] 4L, 30C

Now we change the DOS format back to UNIX format to see the effect.

[Email protected] ~]# Dos2unix job.sh
dos2unix:converting file job.sh to UNIX format ...
[Email protected] ~]# ./job.sh

Can be executed, no longer reported "-bash:./job.sh:/bin/sh^m:bad Interpreter: There is no file or directory" This is wrong.
[Email protected] ~]#

Example examples of using dos2unix-k and Dos2unix-n

[Email protected] ~]#Cat <<eof >1.txt
> 1
> 2

> 3
> EOF
[Email protected] ~]#file 1.txt
1.txt:ascii text
[Email protected] ~]#ls-l 1.txt
-rw-r--r--1 root root 6 11-14 09:08 1.txt
[Email protected] ~]#Date
Sunday, November 14, 2010 09:28:42 CST
[Email protected] ~]#unix2dos-k 1.txt<== Keep File timestamps
unix2dos:converting file 1.txt to DOS format ...
[Email protected] ~]#ls-l 1.txt
-rw-r--r--1 root root 9 11-14 09:08 1.txt
[Email protected] ~]#dos2unix-n 1.txt 2.txt <== converting 1.txt to 2.txt
dos2unix:converting file 1.txt to file 2.txt in UNIX format ...
[Email protected] ~]#ls-l 1.txt 2.txt
-rw-r--r--1 root root 9 11-14 09:08 1.txt
-rw-r--r--1 root root 6 11-14 09:30 2.txt
[Email protected] ~]#file 1.txt 2.txt
1.txt:ascii text, with CRLF line terminators
2.txt:ascii text
[Email protected] ~]#cat-v 1.txt
1^m
2^m
3^m
[Email protected] ~]#cat-v 2.txt
1
2
3
[Email protected] ~]#


This article is from the "XFICC" blog, make sure to keep this source http://xficc.blog.51cto.com/1189288/1550477

Linux command Dos2unix-convert DOS format text files to UNIX format usage notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.