top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

Writer's picturePaul Troncone

Converting between Windows and Linux line breaks

Updated: May 22, 2021

The Windows and Linux operating systems have a slight variation in how they handle newlines in files. Typically, in the Windows environment a line is terminated with the two characters \r\n. The \r character represents a carriage return, and \n represents a newline. In Linux, only the \n character is used to terminate a line.


This causes some interesting behavior issues when moving between the Windows and Linux environment. For example, if you have a multi-line text file that was created in Linux, and then try to open it using a program such as Windows Notepad, the entire contents of the file will appear on a single line.



To fix this you can use a simple one-liner in your Linux terminal of choice to convert back and forth between line break types.


Converting from Linux to Windows Line Breaks


You can use the sed command to convert the file fileLinux.txt to Windows line breaks:


The -i option tells sed to write the results back to the input file. The s is sed's substitute command. The $ is a regular expression that matches the end of a line, and \r is the carriage return.


If you have multiple files that you need to convert you can can couple this with the find command:


The find -type f command will find all files at and below the current working directory. The -exec option will then execute sed for each file that is found. The '{}' will be replaced with the path of each file that is found. The characters \; terminate the exec expression.


Converting from Windows to Linux Line Breaks


To convert from Windows to Linux line breaks you can use the tr command and simply remove the \r characters from the file.


The -d option tells the tr command to delete a character, and '\r' specifies the character to delete. The input to tr is redirected from the file fileWindows.txt, and the output is redirected to the file fileLinux.txt.


Similarly, you can use sed to substitute the \r character with nothing, which causes it to be removed from the file.


44,279 views1 comment

1 Comment


thomas.mauray.bailey
Apr 06

Handy article. Simple, straightforward, useful.


Is there a reason that you did not mentioned dos2unix and unix2dos?

Edited
Like
bottom of page