Find Out Text File Line Endings – LF or CLRF

A newline (also known as a line ending, end of line (EOL) or line break) is a character used to represent the end of a line of text and the beginning of a new line.

Text files created on DOS/Windows machines have different line endings than files created on Unix/Linux.

This note shows how to find out the line endings in a text file and how to change them.

Cool Tip: How to break lines and insert new lines using the echo command from the Command Prompt (CMD) and Windows PowerShell! Read more →

Line Endings

Find Out Line Endings

To find out whether a file uses LF or CRLF line endings, you can use the file command:

$ file <filename>

If the file has the Unix/Linux-style newline characters (\n or LF), it will be displayed as:

file.txt: ASCII text

If it has the DOS/Windows line endings (\r\n or CRLF), you will see:

file.txt: ASCII text, with CRLF line terminators

Display Line Endings

To show the end of line (EOL) characters in a text file you can also use the cat command:

$ cat -e <filename>

The Unix/Linux line breaks (\n or LF) will be displayed as $:

line1$
line2$

The DOS/Windows line breaks (\r\n or CRLF) will be displayed as ^M$:

line1^M$
line2^M$

Cool Tip: How to add a newline at the EOF on save in Sublime Text! Read more →

Change Newline Characters

The line endings in a text file can be converted from Unix/Linux format to DOS/Windows format and vice versa using the dos2unix utility.

To convert the newline characters from DOS/Windows to Unix/Linux:

$ dos2unix <filename>

To convert the newline characters from Unix/Linux to DOS/Windows:

$ unix2dos <filename>

Leave a Reply