Line breaks in Linux

Source: Internet
Author: User

Note : This article is only for learning Exchange, reprint please indicate the source, welcome reprint!

when a TXT file written under Windows is opened under Linux, it is found that there are more ^m at the end of each line, and a TXT file written by Linux is opened in Windows, and all the content is displayed on a single line, originally under Linux, Why is this happening? In fact, this is not the same as the logo of Windows and Linux for line breaks.

Before introducing the line breaks for both systems, the concept of carriage return and line wrapping is introduced.

Enter : CR, shown in the text as ^m, in the string corresponding to ' \ R ', R for return;

newline : LF, shown in the text as $, corresponds to ' \ n ' in the string, and N represents next.

windows break line characters

Windows feels that wrapping should do two things: the first is to run the read-write needle to the next line ( line break ), and the second is to read and write ( carriage return ) from the beginning of the next line, so Windows always uses CR+LF to indicate the meaning of line break, the character is ' \r\ N '.

Break -through characters in Linux

Early UNIX idle with two characters (CR+LF) indicates a newline problem, only the latter LF (Mac system is unique, the former CR is used to denote line break). So Linux uses a newline character of ' \ n '.

Summary:the break character in window is ' \ r \ n ', the break-break character in Linux is ' \ n ', and the Mac's break-break character is ' \ R '.

Since Linux and Windows have inconsistent break characters, the problem comes. When the two are reading the problem, there will be a problem at the beginning of this article:when the Linux system reads TXT files written under Windows, it will have one more ^m at the end of each line(that is, "\ r") While the TXT file written under Linux is read under the Windows system, all the content is on the same line (because the \ r is missing).

A set of commands is provided under Linux to convert the files under Linux to each other in Windows.

Dos2unix Command: Will Windows file-->linux file;

unix2dos Command:---The Linux file to >windows file;

Two parameters:

-K: (keep) retain the original file last modified time parameters, that is, the conversion is not modified content, mtime unchanged;

-N old filename New file name: (new) keep the old file and output the converted file to the new file.

Notice: When reading Windows files under the Linux system, be sure to use the Dos2unix command to convert the files written under Linux to Windows files, you must first use Unix2dos conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.