Jul 212009

When you sometimes copy a file from Windows to UNIX/Solaris or even Linux systems, you can find these anooying ^M characters everywhere. This is because, the file from Windows is in DOS (ASCII) format and needs to be converted to ISO format.

There are many ways to do this. Let’s start with the easy one look at each of them

1. Use dos2unix utility

Solaris pre-installs dos2unix utility into the system to do this job for you. As the name says, it converts the file from DOS format to UNIX format. To do this the syntax is

# dos2unix <file> <new file>

for instance

# dos2unix test.txt test2.txt

Where the test.txt is the file you want the ^M characters removed and test2.txt is the file stripped of the ^M characters.

You may also overwrite the existing file by mentioning the source and destination files as same:

# dos2unix test.txt test.txt

2. Using VI

Open the file with “vi” editor and type the following:


NOTE: To get the ^M in there, you should type CTRL+V+M

3. Using “tr” utility

“tr” utility is used to translate characters. Using with “-d” deletes a listed string.

# tr -d ‘\r’ <old.file > <new.file>

Alternatively, use its octel representation as follows:

# tr -d ‘\015’ <old.file > <new.file>

  10 Responses to “How to remove ^M characters from file in Unix/Solaris”

  1. fine these all are working ..thanks

  2. Hi,

    One more ethod to remove the ^M characters from file in unix.
    Source File = test.txt
    new file= Test1.txt
    cat test.txt| col -b > Test1.txt


  3. Hi,

    I used the method of Nitu, it works excellent. It’s exactly what I was looking for


  4. All 3 method suggested by the article didn’t worked for me this time. Although I’ve tried them several years back and knew they are working.

    Nitu’s method saved my day.


  5. @Ulysses

    impossible that not even item 2 didn’t worked for you. surely you’re doing something wrong.

  6. dos2unix is standard Unix utility. It should not give any problem at all

  7. How do we check without entering vi.
    Can we remove ^M with sed

  8. You can do a

    cat -ev

    to check ..

  9. I always used dos2unix *.sh to remove the ^M character. This time the same command comes up with
    “usage: dos2unix [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ] Error. I have 15 files like these.

    Previously I used dos2unix script.sh
    dos2unix: converting file script.sh to UNIX format …

    how to go about it?

  10. dos2unix on Solaris works differently than dos2unix on (say) Linux. On Linux, it will default to replacing linebreaks in the designated file. On Solaris, you much specify an input and an output file, i.e. dos2unix filea.txt fileb.txt

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>