Pin Me

Convert a Linux Text File to Windows: Using Kate & Command Line

written by: Tolga BALCI•edited by: Lamar Stonecypher•updated: 2/26/2010

It is annoying seeing a text file created in Linux become garbled in Windows. Linux can see and process text files created in Windows correctly, but it is not correct the other way around. Let's see how to overcome this problem.

  • slide 1 of 4

    Introduction

    A Garbled Text File Windows and Linux handle text files differently in terms of line breaks. Windows uses (invisible) \r \n characters (carriage return and newline) to represent where the line ends and a new line starts, however, Linux (and all Unices) use \n character for the same purpose. This is the point which makes Linux-created text files look garbled in Microsoft Windows.

    There are of course several ways to convert Linux text files to Windows. Intermediate/advanced users may find using sed or awk a perfect tool to convert the files, but in this article I will go with easier solutions.

  • slide 2 of 4

    Kate

    KDE's text editor, Kate, has built-in functions to save the text file in Windows format. To do this, open your text file in Kate, go to Tools → End of Line → Windows/DOS. Kate will automatically change all the line breaks and you will be able to save your file in a Windows-proof format.

    If you are carrying text files between Linux and Windows computers frequently, then doing the above may not be your perfect solution. In this case, I recommend telling Kate to save the line breaks as Windows/DOS compliant for every file. Go to Settings → Configure Kate and on the left pane under the “Editor Component" part select “Open/Save" option. In the right pane, select “DOS/Windows" as the “End of Line" and un-tick the “Automatic end of line detection." Click “Apply" and then “OK" to exit.

    If you are using Gnome or any other window manager, you can use Kate without any problems.

  • slide 3 of 4

    Command Line (todos/fromdos)

    Using todos A Garbled Text File todos and fromdos are powerful Linux command line tools that take the filename as their arguments and change the file to DOS format. Both programs are installed together with the tofrodos (or tofromdos) packages; just use your distribution's package manager.

    For our purposes, we will use the todos program. Open up a terminal, change your directory to where your text file is (let's say your text file is named text_file.) Issuing a todos -b text_file command will change all the line breaks in the text_file and make it compliant with Windows environments. To convert it back to Linux format, you can issue fromdos -b text_file. The -b option tells the command to make a backup of the file, so if something goes wrong or you want to revert to the original, you have it readily available. The backed up file has a .bak extension. But I do not think you should worry about this because Linux reads Windows text files without any problems.

  • slide 4 of 4

    Conclusion

    Handling text files differently has something to do with how the operating system is designed, not a flaw. And it is very easy to deal with as we have discussed. As a BrightHub Tip, if you are helpless and have a file that Windows Notepad shows garbled, open it Wordpad. Wordpad handles Linux text files without problems.

    If you want to learn more about the Linux text files and find resources to help you convert Linux files for Windows, then take a look at this article by Rob Dubas, titled 'All Text Files Aren't the Same! How to Easily Convert Linux Files to Windows'