HowTo: UNIX / Linux Convert DOS Newlines CR-LF to Unix/Linux Format

How do I convert DOS newlines CR/LF to Unix/Linux format?

To converts text files between DOS and Unix formats you need to use special utility called dos2unix. DOS text files traditionally have carriage return and line feed pairs as their newline characters while Unix text files have the line feed as their newline character.

ADVERTISEMENTS

UNIX/Linux Commands

You can use the following tools:

  • dos2unix (also known as fromdos) – converts text files from the DOS format to the Unix
    format
  • unix2dos (also known as todos) – converts text files from the Unix format to the DOS format.
  • sed – You can use sed command for same purpose
  • tr command
  • Perl one liner

Task: Convert DOS file to UNIX format

Type the following command to convert file called myfile.txt:
$ dos2unix myfile.txt

However above command will not make a backup of original file myfile.txt. To make a backup of original file. The original file is renamed with the original filename and a .bak extension. Type the following command:
$ dos2unix -b myfile.txt

Task: Convert UNIX file to DOS format

Type the following command to convert file called myfile.txt:
$ unix2dos myfile.txt
$ unix2dos -b myfile.txt

Task: Convert Dos TO Unix Using tr Command

Type the following command:

tr -d '\r' < input.file > output.file

Task: Convert Dos TO Unix Using Perl One Liner

Type the following command:

perl -pi -e 's/\r\n/\n/g' input.file

Task: Convert UNIX to DOS format using sed command

Type the following command if you are using bash shell:
$ sed 's/$'"/`echo \\\r`/" input.txt > output.txt
Note: sed version may not work under different UNIX/Linux variant,refer your local sed man page for more info.

Task: Convert DOS newlines (CR/LF) to Unix format using sed command

If you are using BASH shell type the following command (press Ctrl-V then Ctrl-M to get pattern or special symbol)
$ sed 's/^M$//' input.txt > output.txt
Note: sed version may not work under different UNIX/Linux variant, refer your local sed man page for more info.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
24 comments… add one
  • bruce wolford Jan 4, 2007 @ 2:10

    Howto: UNIX or Linux convert DOS newlines CR-LF to Unix/Linux format

    THANK YOU! to who ever wrote this. I’ve been messing around with this for quite a while. This even works on AIX’s legacy version of sed. My day has been so made by this little nugget.

  • nigel Feb 2, 2007 @ 13:42

    I had tried the suggestion from Wikipedia and that did not work. This solution worked perfectly first time. THANK YOU.

  • John Cairns Sep 5, 2007 @ 14:44

    Your UNIX to DOS sed script is incorrect if what you want is CRLF.

    $ echo | sed ‘s/$'”/`echo \\\r`/” | hexdump
    0000000 0a0d

    ASCII 0/13 is decimal 013, hex 0d, octal 015, bits 00001101: called ^M, CR
    Official name: Carriage Return

    ASCII 0/10 is decimal 010, hex 0a, octal 012, bits 00001010: called ^J, LF, NL
    Official name: Line Feed

    For CRLF you want 0d0a. Your producing LFCR here.

  • Calvin Smith Dec 12, 2007 @ 6:23

    Dear John,

    Actually, the scripts are correct. Hexdump is reversing the byte order (integer format)

    Try this for example and as a simplified script:

    $ echo TEST | sed ‘s/$/\r/’ | hexdump -C

    Then try it without the -C.
    You will notice that the 54’s (T) are next to each other. (ETTS)

  • David Schiavone Aug 13, 2009 @ 10:49

    Impressive i would say.. I used dos2unix on CentOS 5.3 without any problems and a file of 300,000 got modified in less then 15 secs… compared it to using notepad++ on windows and your computer would freeze up for at least 2 mins for the same file ;).

  • Dzmitry Lazerka Nov 13, 2009 @ 9:33

    sed ‘s/$'”/`echo \\\r`/” input.txt > output.txt
    leaves one extra CR at the end of the file.
    This makes the suggestion broken.

  • Allan Oct 5, 2010 @ 14:25

    The sed … echo \\\r worked fine for us on AIX version 6.1 to convert Unix to DOS. Thanks!

  • sankar Nov 10, 2010 @ 8:12

    I have to convert 50 files at one shot to Unix format. Is there a way to do it?

    • 🐧 nixCraft Nov 10, 2010 @ 10:47

      Use bash for loop:

      for f in /path/to/src/*
      do 
        dos2unix "$f"
      done
      • Steen Jan 29, 2012 @ 22:50

        Even easier:
        $cd dir/with/stuff
        $dos2unix *

        • Mad Physicist Dec 21, 2016 @ 20:31

          Even easier:

          $dos2unix dir/with/stuff/*

  • Sunil Mar 3, 2011 @ 1:27

    Do we have dos2unix equivalent command on Window OS. Ie. A Window command to convert dos newlines to unix format.

    Thanks for your help.

  • Steen Jan 29, 2012 @ 22:49

    Thanks!
    I was just about to write a shellscript for this, but then I just googled it because I forgot if it was /n to /r or how :P It works!!!

    Side note:
    For people with problems about duplicates moved from Windows to Linux on Garry’s Mod servers they should use “dos2unix STEAM*/*.txt” to avoid trying to convert directories. There’s a lot of trouble with user created directories otherwise. The program tries to convert directories – which is a bit annoying.

  • kevin Jul 13, 2012 @ 15:54

    I found the sed unix to dos to not work, it ended up leaving CRs at the end of lines and CRLF at the start of every line….

    instead the following correctly left CRLFs at the end of every line (that originally had LFs)

    sed “s/$/`echo`/” on Ubuntu BASH

  • Ayyaz Jul 27, 2012 @ 17:45

    I want to know that can a DOS (Linux) support the Microsoft widow XP. if yes then how?

  • vinil Oct 28, 2012 @ 13:10

    how o convert DOS CR/LF files to UNIX files in which lines end in LF?

  • simhe Feb 14, 2013 @ 8:27

    Task: Convert Unix TO Dos Using Perl One Liner

    perl -pi -e ‘s/\n/\r\n/g’ input.file

  • Cyrus May 13, 2013 @ 1:09

    For me, `tr -d ‘\r’ output.file` deletes all the carriage returns without replacing them with newlines. `tr ‘\r’ ‘\n’ output.file` works nicely, though

  • Josiah Yoder Jul 24, 2013 @ 16:20

    You might also suggest
    sed ‘s/$/\r/’
    This works fine on Cygwin sed, though perhaps not for that pesky last line.

    Thanks for the suggestions.

  • cee Nov 2, 2014 @ 8:56

    You could also open the file in vim and typ .
    :ff=unix

  • Steph Apr 17, 2015 @ 13:52

    Excellent article, my favorite method are with tr -d ‘\r’ or simply with vi you do
    :s/^M//g , why I like those is because there are realy simple to remember and work on all environment, unix, linux, aix , solaris etc…

    I try the sugestion of cee , :ff=unix , it’s very cool , the only problem it’s seems it only work on vim , not in vi . Where I work there is no vim , snif , snif.

  • Stefan Sjöberg Apr 30, 2016 @ 18:02

    I found a very easy way… Open file with nano
    # nano file.txt
    press Ctrl+O to save, but before pressing Enter press:
    Alt+D to toggle betwen DOS and Unix/Linux line-endings, or:
    Alt+M to toggle betwen Mac and Unix/Linux line-endings
    then press Enter to save and Ctrl+X to quit.

  • iff Sep 27, 2016 @ 6:10

    What about ancient Unix utility col(1)?

  • Addams Scrub Apr 6, 2017 @ 12:27

    Well, sed does not always work. tr it sure does.
    Thank you for your tutorial.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.