A line ending is one or two characters used in text files to indicate the end of a line and thus the beginning of the next line.
Due to historical reasons involving printers, there are two characters involved in newlines on different platforms: line feed (LF) with ASCII code 0x0A
and carriage return (CR) with ASCII code 0x0D
. In C code, LF is \n
and CR is \r
. A trick to remembering this is that \r
has the r from carriage return.
There are currently two types of line endings based on the two main operating systems. Linux and Mac OS X systems use the Unix line ending, which is LF
. Windows systems use the DOS line ending, which is CR+LF
. Classic Mac systems used to use the Mac line ending, which is CR
.
Text files with Unix newlines end up on Windows and files with DOS newlines end up on Linux. I like to use the file
tool to check which type of text file I am dealing with:
$ file windows.c
windows.c: C source, ASCII text, with CRLF line terminators
$ file linux.py
linux.py: Python script, ASCII text executable
$ dos2unix windows.c
$ unix2dos linux.py
$ find . -type f | xargs dos2unix -ic
The CR character in DOS files is rendered as ^M
at the shell or in Vim, so looking for that character is another way to find DOS files.
$ find . -type f | xargs dos2unix -ic | xargs dos2unix