Convert windows file to linux

Ubuntu Genius’s Blog

Cool UBUNTU Tips & Tricks brought to you by OzzyFrank

How to Convert Windows/DOS Text Files to Linux/UNIX Format

A Quick Intro to End-Of-Line

Most people don’t realise that when they hit the Enter key to create a new paragraph in a text file, something very different is going on behind the scenes in the three major operating systems: Windows , Macintosh and Linux . The “end-of-line delimiter” (often expressed as “End-Of-Line“, “End of Line“, or just “EOL“) – which some of you know as the “line break” or “newline” – is a special character used to designate the end of a line within a text file .

UNIX -based operating systems (like all Linux distros and BSD derivatives) use the line feed character ( \n or ), “classic” Mac OS uses a carriage return ( \r or ), while DOS/Windows uses a carriage return followed by a line feed ( \r\n or ). Now that Mac OS X is based on FreeBSD‘s file system, it follows the UNIX convention.

Now, the reason most people don’t know about all this is because nobody really should have to. But while users of Linux distros and Mac OS can open Windows text files in basically any available editor and not even know the difference, the same can’t be said for Windows users opening files created in one of the other operating systems.

If you type up a simple text file in Ubuntu and save it in the default “ Unix/Linux ” format, in Windows it will appear as one continuous paragraph, with black squares where the line breaks (or new paragraphs) should be. While you can open the file in a more advanced text editor (or proper word processor) to view it as it should look, others you’ve sent it to are just likely to double-click it and let it open in Notepad (which can only handle MS-DOS EOL).

Occasionally, the reverse is the issue, but you can convert Windows text files to UNIX easily with Gedit , as well as convert them via the terminal, so hopefully the following guide will be of use.

For more detailed info on End-Of-Line , go to the Wikipedia page.

Converting Windows EOL to Linux via the Terminal

If you find the text editor you’re using to display Windows files in Ubuntu shows ^M instead of a line break (not very likely with even the most lightweight text editors, but something you’ll probably come across if you display the text in a terminal), don’t worry – just convert them to Unix/Linux format.

Читайте также:  What are linux containers

While you can actually open them in Gedit and use Save As… to save over them (or to create copies) in the correct format, for more than a couple of files this would be the long, complicated solution.

By far the quickest and easiest approach is to convert the offending files via the command-line. This way, you could batch-convert hundreds of such files at once, not have to do them individually.
There are actually quite a few ways to do this, but we’ll look at a couple of tiny packages you can install, and the related commands to use.

The first – the tofrodos package – is undoubtedly the most widely-used, so we’ll look at that in detail – especially since many of the guides out there are outdated, since the commands it contains have been renamed.

The second is a little package called flip , and since it’s tiny and won’t cause any issues, it’s worth installing as a backup (just in case. I found it useful after trying to get tofrodos going on a new system, before I found out the commands were changed).

There is no actual command tofrodos , as it is just the package that contains the commands todos and fromdos . Currently, the vast majority of online guides will list the commands as unix2dos and dos2unix , but as the developer states:

With this release the symlinks “unix2dos” and “dos2unix” are dropped from the package. This will allow the introduction of the original dos2unix package, which also supports conversion to MacOS style files.

So now you can choose to use either todos (to convert to Windows ) and fromdos (to convert to Linux ), or just fromdos with options ( fromdos -u to convert to DOS , and fromdos -d to convert to UNIX , though obviously the -d option really isn’t needed, as it is the default behaviour for the fromdos command).

We’ll use fromdos , as it is easier to remember, and show how to alter a single file, or all text files in a given folder. When you’re ready to proceed, open a terminal in the folder containing the text file(s) and use one of the following commands (note that for the purpose of illustration, the .txt suffix is used, but you can specify any other extension for your text files).

To Convert to UNIX/Linux via Terminal :

Single file (remember to replace filename.txt with the actual name of the file)

fromdos filename.txt

All text files in a folder (if the extension differs to .txt, simply replace it in the command)

fromdos *.txt

Similarly, flip is easy to use:

flip -u filename.txt (or flip -u *.txt for multiple files)

Converting Windows EOL to Linux with Gedit

It’s actually very easy to convert text files with Windows EOL to Unix/Linux in Ubuntu using the default Text Editor , Gedit . Simply open the files, choose Save As…, go to Line Ending in the dialogue box and choose Unix/Linux instead of Windows . While that is easy enough, for more than one or two you’d really want to save yourself some time and hassle and perform a batch-conversion via the terminal.

Читайте также:  Linux compress folder with zip

Did this information make your day? Did it rescue you from hours of headache? Then please consider making a donation via PayPal, to buy me a donut, beer, or some fish’n’chips for my time and effort! Many thanks!

Buy Ubuntu Genius a Beer to say Thanks!

Источник

How to Convert a Windows File to a UNIX File

You may ask yourself, what is the purpose of this article? Why convert a Windows file to adapt to a UNIX environment like Linux? Isn’t Linux all-powerful? The exceptional capabilities of the Linux operating system do not spare it from incompatible displays of files transferred from other computing platforms.

Just because you can open a file on a Linux environment does not imply that you have full control over how the file’s texts should be displayed.

You will encounter instances where a file’s texts or words are jammed together on a single giant line. In other instances, the same file texts’ displays might not have line breaks or sentence spacing.

A common attribute of raw Windows files opened in UNIX systems like Linux is the unavoidable end-of-line display of ^M or Ctrl-M characters.

This article guide seeks to achieve one objective; the conversion of a Windows File to a UNIX file without changing the format of the resulting file.

Ways to Convert Windows File to Unix Format in Linux

We can achieve the objective of our article through several methods. These methods allow us to convert a Windows file to a UNIX file and still retain the original format of the Windows file.

Convert Windows File to Unix Using dos2unix Command

Depending on your Linux operating system distribution, you can install the dos2unix command-line tool from one of the following commands:

$ sudo apt-get install dos2unix [On Debian, Ubuntu and Mint] $ sudo yum install dos2unix [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/dos2unix [On Gentoo Linux] $ sudo pacman -S dos2unix [On Arch Linux] $ sudo zypper install dos2unix [On OpenSUSE]

The command syntax for using the dos2unix tool is as follows:

$ dos2unix Your_Windows_File Final_Unix_File

So if you have a sample file created on a Windows computing system and want to open it on a Linux computing system without compromising its format, you would use the following command.

$ dos2unix windows_readme.txt unix_readme.txt

Before we run the above command, we need to create a blank unix_readme.txt file that will accommodate the converted file.

$ touch unix_readme.txt $ dos2unix windows_readme.txt unix_readme.txt

Convert Windows File to Unix Using dos2unix

As per the screen capture, your converted Windows file should comfortably adapt to any Unix environment.

Using AWK Command – Convert Windows File to Unix

The awk command is pre-installed on all modern full-fledged UNIX computing systems like Linux. To convert our sample Windows file, we would implement the awk command in the following manner:

$ awk '< sub("\r$", ""); print >' windows_readme.txt > new_unix_readme.txt

Awk Convert Windows File to Unix

As you have noted, with the awk command, we don’t need a pre-existing blank Linux file to accommodate the converted Windows file. The command creates and populates the Unix file version of the Windows file on its own.

Читайте также:  Open source vpn linux

Using tr Command – Convert Windows File to Unix

If most of the Windows files you open on your Linux environment have unnecessary Ctrl-Z and carriage return characters, then you will appreciate what the tr command has to offer.

Supposing our sample Windows file is a victim of such characters, removing them will require implementing the following command:

$ tr -d '' < windows_readme.txt >polished_unix_readme.txt

tr Convert Windows File to Unix

The inbuilt nature of the tr command also generates the resulting UNIX file without the need for its pre-creation.

The flexibility of the three discussed approaches to converting any editable Windows file to UNIX file format should save you from the headaches of having to manually edit your downloaded or transferred Windows files to remove unwanted characters and spaces while on a Linux environment.

Источник

How to convert a text file from Windows to Unix

When converting from Unix to Windows, I get the correct output; however, when going from Windows to Unix I get some strange output. I thought all I had to allow for was the removal of the carriage return, ‘\r’. This isn’t working though. When I open the text file after running the code, I get some strange results, the first line is correct, and then all hell breaks lose.

 int main( ) < bool windows = false; char source[256]; char destination[256]; // Allocate the max amount of space for the filenames. cout > source; ifstream fin( source, ios::binary ); if ( !fin ) // Check to make sure the source file exists. < cerr //endif cout > destination; ifstream fest( destination ); if ( fest ) // Check to see if the destination file already exists. < cout //endif >//endif clrscr(); // Clear screen for neatness. ofstream fout( destination, ios::binary ); if ( !fout.good() ) // Check to see if the destination file can be edited. < cout //endif // Open the destination file in binary mode. fout.open( destination, ios::binary ); char ch = fin.get(); // Set ch to the first char in the source file. while ( !fin.eof() ) < if ( ch == '\x0D' ) // If ch is a carriage return, then the source file < // must be in a windows format. windows = true; >//endif if ( windows == true ) < ch = fin.get(); // Advance ch, so that at the bottom of the loop, the >//endif // carriage return is not coppied into the new file. if ( windows == false ) < if ( ch == '\x0A' ) // If the file is in the Unix format.. < fout.put( '\x0D' ); // When a new line is found, output a carriage >//endif // return. >//endif fout.put( ch ); ch = fin.get(); >//endwh if ( windows == true ) < fout.put( '\x0A' ); >//endif fout.close(); fin.close(); // Close yer files. if ( windows == true ) // A little output for user-friendly-ness. < cout else< cout //endif cout //endmn 

I doubt it's your problem in this case, but you really should call fest.close() to close the input stream for the destination file before then opening it up as an output stream too.

I vote for a move to codereview. This vote is partially motivated because I want codereview to flourish.

Источник

Оцените статью
Adblock
detector