DEV Community

theBridge2
theBridge2

Posted on

4

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more