The most annoying thing about transferring files to a Linux system when using Windows is to file different modifications. I have also written an article about Excel files copied to Linux under the problems caused by. VI View the Space, tab, and other special symbols, and this article can be a follow-up bar! Originally this article I mainly want to talk about the Dos2unix and Unix2dos. I would like to write down, later on the internet to see someone summed up very good, simply copycat. As below:
Purpose description
The Dos2unix command is used to convert a DOS-formatted text file to a UNIX-formatted (Dos/mac to UNIX text files format converter). The text file in DOS is marked as a break mark in Rn, and is represented as 16 in 0D 0 A. The text file under UNIX is marked with n as the break mark, which is represented as 16 in 0 a. DOS format text files under Linux, open at the end of a lower version of VI will show ^m, and many commands are not very good to handle this format file, if it is a shell script. Text files in UNIX format are displayed together when opened with Notepad in Windows. As a result, there is a need to convert two format files to each other, and the corresponding conversion of UNIX-formatted text files into DOS format is the Unix2dos command.
Common parameters
The simplest way to convert a DOS format text file into a UNIX format is to dos2unix the file name directly.
Format: Dos2unix file
If you convert multiple files at once, follow these file names directly after Dos2unix. (Note: can also add-o parameter, also can not add, the effect is the same)
Format: Dos2unix file1 file2 file3
Format: Dos2unix-o file1 file2 file3
The above is converted directly on the original file, if you want to save the results of the transformation in other files, and the source file does not change, you can use the-n parameter.
Format: Dos2unix oldfile newfile
If you want to keep the file timestamp unchanged, add the-k argument. So a few of the above commands are able to add a-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 manner to the Dos2unix command.
Using the sample
Example a DOS format text file performance under Linux
Now there is a script file job.sh, which is edited with VI under Linux.
[Root@jfht ~]# Cat job.sh
#!/bin/sh
Date >job.txt
Now convert it to a DOS format text file.
[Root@jfht ~]# Unix2dos job.sh
unix2dos:converting file job.sh to DOS format ...
Try to run a bit.
[Root@jfht ~]#./job.sh
-bash:./job.sh: Insufficient permissions
[Root@jfht ~]# chmod +x job.sh
[Root@jfht ~]#./job.sh
-bash:./job.sh:/bin/sh^m:bad Interpreter: No file or directory
The DOS script file cannot be interpreted because the first line of the script file is used to specify the interpreter, and the Linux system thinks the interpreter is/bin/sh^m rather than/bin/sh.
Let's take a look at the DOS format file with some commands under Linux.
[Root@jfht ~]# cat-v job.sh <== cat-v can see nonprinting characters in the file without the cat command with the-v argument.
#!/bin/sh^m
^m
Date >job.txt^m
^m
[Root@jfht ~]# hexdump-c job.sh <== hexdump-c can see the hexadecimal representation of each byte of the file.
00000000 2f, 6e 2f 0a0d 0d 0a |#!/bin/sh....dat|
00000010 3e 6a 6f-2e-|e-0d-0a 0d 0a->job.txt....|
0000001e
[Root@jfht ~]# VI job.sh <== use VI to open when you can see the following [DOS] format hint. Some versions vi show 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.
ROOT@JFHT ~]# Dos2unix job.sh
dos2unix:converting file job.sh to UNIX format ...
[Root@jfht ~]#./job.sh
Can be executed, no longer reported "-bash:./job.sh:/bin/sh^m:bad Interpreter: No file or directory" This is wrong.
[Root@jfht ~]#
Examples of the use of Example II Dos2unix-k and Dos2unix-n
[Root@jfht ~]# cat <<eof >1.txt
> 1
> 2
> 3
> EOF
[Root@jfht ~]# file 1.txt
1.txt:ascii text
[Root@jfht ~]# ls-l 1.txt
-rw-r--r--1 root 6 11-14 09:08 1.txt
[Root@jfht ~]# Date
November 14, 2010 Sunday 09:28:42 CST
[Root@jfht ~]# unix2dos-k 1.txt <== Keep file timestamp
unix2dos:converting file 1.txt to DOS format ...
[Root@jfht ~]# ls-l 1.txt
-rw-r--r--1 Root 9 11-14 09:08 1.txt
[Root@jfht ~]# dos2unix-n 1.txt 2.txt <== convert 1.txt to 2.txt
dos2unix:converting file 1.txt to file 2.txt in UNIX format ...
[Root@jfht ~]# ls-l 1.txt 2.txt
-rw-r--r--1 Root 9 11-14 09:08 1.txt
-rw-r--r--1 root 6 11-14 09:30 2.txt
[root@jfht ~]# file 1.txt 2.txt
1.txt:ascii text, with CRLF line terminators
2.txt:ascii text
[Root@jfht ~]# cat-v 1.txt
1^m
2^m
3^m
[Root@jfht ~]# cat-v 2.txt
1
2
3
The Cat-v command and Hexdump in this article also apply to VI view space, tab, and other special symbols in this section is not import errors in the view. and VI in the No list effect is the same. It can also be used in conjunction with. Of course hexdump in hexadecimal is the best illustration of the problem.
R 0D
N 0A
T 09
Dos/windows's newline character Rn, which is hexadecimal, is 0D 0A
Linux/unix the newline character N, which is the hexadecimal representation of 0A
The tab in Excel is T