The UNIX and DOS operating systems (which includes Microsoft Windows) differ in the format in which they store text files. DOS places both a line feed and a carriage return character at the end of each line of a text file, but Unix uses only a line feed character. Some DOS applications need to see carriage return characters at the ends of lines, and may treat Unix-format files as giant single lines. Some Unix applications won't recognize the carriage returns added by DOS, and will display Ctrl-m characters at the end of each line. This appears on the screen as ^M.
There are many ways to solve this problem. In this document we provide instructions on how to use FTP, screen capture, unix2dos and dos2unix, tr, awk, Perl, and Emacs, to do the conversion. Before you use these utilities, the files you are converting must first be on a Unix computer.
When using an FTP program to move a text file between Unix and DOS, be sure the file is transferred in ASCII format. This will ensure that the document is transformed into a text format appropriate for the host. Some ftp programs, especially graphical applications like Rapid Filer, do this automatically. If you are using ftp from the Unix or DOS prompt, however, before you begin the file transfer, be sure to enter at the ftp prompt:
You can also convert files from Unix to DOS format when transferring them to a PC with a communications program by selecting ASCII text download. Select this option with your communications program to capture all the text subsequently displayed to your screen, and then enter at the Unix prompt:
Replace unixfile.txt with the name of the Unix text file you are transferring. Most communications programs will add carriage returns to the stream of text as they save it to your PC's hard drive. Once the file has finished displaying, abort the text download.
Note that this method may be slow for large text files. Also, no error checking is performed on the file as it is transferred. Line noise may corrupt its contents, especially if you are using a terminal connect program such as HyperTerminal.
dos2unix and unix2dos
On systems using SunOS, the utilities dos2unix and unix2dos are available. These utilities provide a straightforward method for converting files from the Unix command line.
To use either command, simply type the command followed by the name the file you wish to convert, and the name of a file which will contain the converted results. Thus, to convert a DOS file to a Unix file, at the Unix prompt, enter:
dos2unix dosfile.txt unixfile.txt
To convert a Unix file to DOS, enter:
unix2dos unixfile.txt dosfile.txt
Note that these utilities are only available on SunOS systems (CDF runs SunOS).
You may use tr to remove all carriage returns and Ctrl-z (^Z) characters from a DOS file by entering
tr -d '\15\32' < dosfile.txt > unixfile.txt
tr cannot be used to convert a document from Unix format to DOS.
To use awk to convert a DOS file to Unix, at the Unix prompt, enter