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

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay