DEV Community

theBridge2
theBridge2

Posted on

Carriage Return line feed: CRLF - Linux vs Windows

CRLF? Carriage return (CR) and line feed (LF) come from the days of typewriters.

History

CR = '\r' = 0x0D (HEX) was used to move the cursor to the beginning of the line

LF = '\n' = 0x0A (HEX) was used to move the cursor down to the next line.

The combination of CRLF in type writers is what moved the cursor from the end of the current row to the start of the next row.

Windows vs Linux file compatibility

As computers developed linux moved away from using both CRLF and just used LF to indicate a new line inside a file while windows continued to use both.

If a file has only '\n' (LF) for its line endings then it is linux compatible.
If a file has '\r\n' (CRLF) for its line endings then it is windows compatible.

To convert from linux to windows use the sed command in a linux terminal:



sed -i 's/$/\r/' fileLinux.txt


Enter fullscreen mode Exit fullscreen mode

To convert from windows to linux, use the following command in a linux terminal:



tr -d '\r' <fileWindows.txt > fileLinux.txt


Enter fullscreen mode Exit fullscreen mode

credit: https://www.commandlinewizardry.com/post/removing-windows-line-breaks

How do I know which line ending a file has?

Notepad ++ does it:

Image description

VsCode will show you what the line endings are for a given file at the bottom right of the screen.

Image description

Top comments (0)