Linux text file and win text file line feed format conversion command

Source: Internet
Author: User

When a script file edited under Win is uploaded to the Linux server, it cannot be executed normally. It was mistakenly identified as a Linux configuration problem and later found that, it is caused by different line breaks during Windows and Linux storage files. The linefeed used in DOS is ^ m $, which is called the CR and LF symbols. In Linux, only the LF ($) line break is available.

You can use the following command to convert the format: $ dos2unix and $ unix2dos. However, these two commands do not exist in the Ubuntu release version. You can run the following command:

$ Sudo apt-Get install tofrodos

Command to install. Then, use the following format again.

[Root @ Linux ~] # Dos2unix [-Kn] File [newfile]

[Root @ Linux ~] # Unix2dos [-Kn] File [newfile]

Parameters:

-K: Retain the original mtime format of the file (the last modification time of the file content is not updated)

-N: Retain the original file and output the converted content to the new file, for example, dos2unix-n old New

Example:

Example 1: update the provided hosts file format to DOS format.

[Root @ Linux ~] # Unix2dos-K hosts

Unix2dos: Converting file hosts to DOS format...

# At this time, the hosts file time will not change, but the content mainly changes the line break to the dos crlf.

Example 2: Change "hosts" in the DOS format to hosts. Dos and convert it to Linux

Format to hosts. Linux

[Root @ Linux ~] # Mv hosts. Dos

[Root @ Linux ~] # Dos2unix-K-n hosts. Dos hosts. Linux

Dos2unix: Converting file hosts. DOS to file hosts. Linux in UNIX format...

[Root @ Linux ~] # Ll

-RW-r -- 1 Root 288 Aug 1 13:30 hosts. Dos

-RW ------- 1 Root 279 Aug 1 :30 hosts. Linux

# Because the DOS format contains more Cr characters, the file size is relatively large.

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.