If a file was written on a Windows based system and is opened by a text editor on a UNIX system, it is very common for the “Ctrl-M” characters (^M) to be displayed at the end of each line of text. If a file was written on a UNIX system and opened by a text editor on a Windows system, the line break character (EOL) may not be displayed correctly. The carriage return character is also different for both UNIX and Windows. While dealing with files, you don’t want to be limited by whether the file was created on Linux or Windows. So how do you convert a file from UNIX to Windows (or vice versa) without having the formatting go all crazy? We’ll walk you through the steps.
Converting Files from Linux/UNIX format to Windows Format
If you’re using a UNIX based system to transfer the files to a Windows system, there are some commands that let you convert the text file(s) you are transferring to a format Windows can understand.
The dos2unix and unix2dos command
You can use command line to safely convert files from UNIX to Windows and vice versa. To convert a Windows text file to a UNIX text file, enter this: The above command converts and replaces “windows.txt” file to “unix.txt”. To convert a UNIX text file to a Windows text file, enter this command: The above command will convert a UNIX created text file called “unix.txt” to a Windows compatible text file called “windows.txt”.
The awk command
The awk command also lets you convert a file from UNIX to Windows and vice versa. To convert a Windows file to a UNIX file, enter the following command: To convert a UNIX text file called “unix.txt” to a Windows text file called “windows.txt”, enter the following command:
The tr command
The tr command (transliterate) can be used to remove the carriage return characters and the “Ctrl-Z” characters from a Windows file. This can only be done if you are converting a file from Windows to UNIX. The command will be written as follows: The tr command transliterates one character with another. In this case, it is helping you omit unnecessary characters.
Using the Visual Editor (Vi)
If you are using the Visual Editor to view a file created on a Windows system, you can remove the carriage return characters by typing the following command line: To get the computer to input the ^M character, you need to hit “Ctrl + v” and then press Return.
Using File Transfer Protocol Programs
File Transfer Protocol (FTP) programs are available both for UNIX and Windows system. If you need to convert a lot of files from Windows to UNIX (or the other way around), then it’s a good idea to download a FTP program. There are many available for free on the Internet. The Hummingbird FTP is one of the more popular FTP programs out there. It is secure and easy to use. Most FTP programs will transfer files from UNIX to Windows in the ASCII format. Sometimes you have to specify the format for yourself (if you are using command line based FTP programs). To do that, just enter this in the command line:
Conclusion
The easiest way to convert a file from a UNIX format to Windows (and the other way around) is to use an FTP program. The conversion commands are your next best bet. If you are looking for additional commands that perform the same task, you can search for perl and sed commands. However, do keep in mind that these commands may not work across all systems.