UNIX vs. Windows End-of-Line Mistery

Time to discuss the infamous end-of-line in UNIX like systems vs. Windows systems. Just in case you do not know what end-of-line is. End of line character is the character that tells the system that the line of the file ends. For example, when you write a text file using a text editor, you may write something like:

This is the first line of the text file
This is the second line of the text file

When editing the file, to indicate to the system that the line is finished, you would press the ‘Enter’ key.

Now, if you always edit files in UNIX and remain in UNIX, no problems will be ever noted. If always editing on Windows, again, no problems will be ever noted.

The issue presents itself should you edit a file in UNIX and want to view it in Windows and vice-versa.

Let’s say that you created the above file on a UNIX system, then in Windows, using notepad, you open the file, you will see:

This is the first line of the text fileThis is the second line of the text file

As you see, everything is on one single line. Should you do the opposite, editing the file in Windows, then open it in UNIX, you will see something like:

This is the first line of the text file^M
This is the second line of the text file^M

Now, let’s explain why there is such a difference.

In UNIX the end of line character is the new-line or line-feed character, it is represented different ways: \n, ASCII(10), LF, etc.

In Windows the end of line is represented by 2 characters, line-feed and carriage-return. Carriage return is represented in different ways also: \r, ASCII(13), CR, etc.

To summarize, in UNIX the end of line is \n, in Windows the end of line is \r\n.

Modern file transfer applications can automatically resolve the problem for you. For example, in FTP or SFTP you can setup the application to automatically correct the end-of-line characters based on the destination platform. Also some modern text editors automatically understand the end of line.

I hope this clears the problem some, but in order to really understand, please experiment in dealing with files on UNIX and Windows.

Leave a Reply