The Dos2unix of Linux commands(2011-09-22 11:24:06)
reproduced
Linux command Dos2unix-convert DOS format text files to UNIX format usage notes
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] ~]# Unix2dos job.sh
unix2dos:converting file job.sh to DOS format ...
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, without the cat command with the-v parameter.
#!/bin/sh^m
^m
Date >job.txt^m
^m
[[email protected] ~]# hexdump-c job.sh <== hexdump-c You can see the hexadecimal representation of each byte of the file.
00000000 2f 6e 2f 73 680d 0a 0d 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 [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 timestamp
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 <== convert 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] ~]#
The Dos2unix of Linux commands