Have you ever noticed that line breaks behave differently on Windows and Linux? Here's how to fix it.

Have you ever sent a text file created on a Linux system to someone complaining about it looking wrong on macOS or Windows? This may be because these systems treat line endings differently. Fortunately, this can be easily fixed with a few utilities: Dos2Unix and Unix2Dos.

What's wrong with line ends?

One difference between Windows and Linux that could make files difficult to exchange is the way line endings are counted. This goes back to the time when computers used teletypewriters for input and output, which were effectively automatic typewriters.

A new line, also called a line feed, advances the paper one line and the carriage return moves to the beginning of the line. Command line interfaces that follow this line break interpretation convention.

Windows inherited the MS-DOS convention of counting a new line and a carriage return as one line, while Linux continued the Unix tradition of counting only line breaks as new lines.

In most cases, text editors are smart enough to recognize files created on different systems and adjust the display accordingly. However, problems can occasionally arise. You might be trying to run a script and the interpreter might throw an error, expecting the lines to end differently.

Using Dos2Unix and Unix2Dos

Fortunately, there are a few utilities that you can use to convert line endings in files between formats. They are called Dos2Unix and Unix2Dos.

Although these utilities are common in many Linux installations, you can use the standard package manager to download them if they are not installed on your system.

To convert a file created on a Windows system to Linux, use dos2unix::

dos2unix file

…Where file is the name of the file you want to convert. This program converts the file from DOS line breaks to existing Unix lines, as the name suggests. If you want to convert a copy of the file, use the -n Possibility:

dos2unix -n file1 file2

…Where File1 and File2 are the names of the Original file and the Output file. The unix2dos command works the same way and uses the same arguments.

An alternative option is to use a text editor on Linux or Windows to convert files, as these can also recognize and change the behavior at the end of a line.

Related: Unix vs. Linux: The Differences Between and Why It Matters

Now your text files will look right on every system

This pair of utilities can be used to ensure that any text file will work on Windows or Linux. Fixing line breaks is now easier than ever with dos2unix and unix2dos.

In the real world, Linux and Windows users often need to share files. Fortunately, this is very easy to do these days.

Transfer and share files between Windows and Linux

Are you switching from Windows to Linux and need to copy data? Use these tricks to transfer or share files from Windows to Linux.

Continue reading

About the author

David Delony
(27 articles published)

David is a freelance writer living in the Pacific Northwest but originally from the Bay Area. He has been a technology enthusiast since childhood. David's interests include reading, watching quality TV shows and movies, retro games, and collecting records.

More
By David Delony

Subscribe to our newsletter

Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.