How To Convert Files from Linux/Unix Format to Windows and vice versa

If you’ve ever transferred a text file from a UNIX based system to a Windows system directly, you know that when you open the text file on the Windows system, it is usually not displayed correctly. Windows based text reader programs (like Notepad) may not be able to display the text. In most cases, when you open the text file, all the words get displayed on a single giant line, without any breaks. This is because there is a slight difference in the way a text document is written (and read) on Windows and UNIX.


If a file was written on a Windows based system and it is opened by a text editor on a UNIX system, it is very common for the “Ctrl-M” characters (^M) being 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:

dos2unix windows.txt unix.txt

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:

unix2dos unix.txt windows.txt

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:

awk '{ sub("\r$", ""); print }' windows.txt > unix.txt

To convert a UNIX text file called “unix.txt” to a Windows text file called “windows.txt”, enter the following command:

awk 'sub("$", "\r")' uniz.txt > windows.txt

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:

tr -d '\15\32' < winfile.txt > unixfile.txt

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:

:1,$s/^M//g

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:

ascii

Conclusion

The easiest way to convert a file from a UNIX format to Windows (and the other way around) is to use a 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.