Linux text dos format to UNIX format, remove ^m symbols

Source: Internet
Author: User

Q: I pass a text file to Linux in Windows via FTP, but after opening the text file, each line has a ^m flag at the end. Because of the long, with the editor to remove too much trouble, what is the solution?

A: In order to solve this problem, Linux has a special two tools to swap Windows format and Linux format, which are Dos2unix and Unix2dos respectively. For example, use the following command to convert a file named "FileName" from the Windows format to the Linux text format.


Under Linux, it is unavoidable to use vim to open some edited text files under Windows. We will find a ^m symbol at the end of each line of the file, because the DOS editor and the Linux editor are inconsistent with the carriage return at the end of the file line.

For a carriage return definition:

There are several ways to remove these symbols more quickly:

(1) is the command with VI:
Open a text file with VI
VI Dos.txt
Command mode input
: Set Fileformat=unix
: W

(2) VI using regular expressions to replace

(3) using the SED tool
Sed ' s/^m//' filename > Tmp_filename

(4) Since the window under the carriage return more ", then of course, by deleting", can also be implemented:
Tr-d ""

(5) The last method is my most commonly used method, personally feel most convenient
Under the Terminal Knock Command:
$ Dos2unix filename
switch directly to UNIX format and it's OK! ~

On the web, we searched for an article that will transform the line between UNIX and Windows, learning to add:
Dos2unix [-kn] file [newfile] transition from DOS to UNIX
Unix2dos [-kn] file [newfile] transitioning from UNIX to DOS or Windows
-K: Retains the original mtime time of the document (does not update the last modified time of the document)
-N: Keep the original document and enter the converted content into the new document for example: Dos2unix-n old New

Examples are as follows:

$cat-v killws#!/bin/bash^madmin_dir= "/home/admin" ^mfunction check_user {^m if [[' whoami '! = ' admin ']];then^m Echo ' Apac He can start by admin user,exit! " ^m exit^m fi^m}^m$dos2unix-k killwsdos2unix:converting file killws to UNIX format ... $cat-v killws#!/bin/bashadmin_dir=  "/home/admin" function check_user {if [[' whoami '! = ' admin ']];then echo ' Apache only can start by admin user,exit! ' exit Fi

Linux text dos format to UNIX format, remove ^m symbols

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.