Is there a Bash command to convert \r\n to \n?

When I upload my scripts from Windows to Linux, I need a utility like this to make things work.

  • 7
    dos2unix is usually available, otherwise sed -e 's/\r$//'
    – falstro
    Jun 24, 2010 at 14:17
  • 1
    No, there's no Bash command for that, but there's dos2unix which is a Unix/Linux program to do what you want. Jun 24, 2010 at 14:27
  • 1
    Why don't you just use a sane text editor that lets you choose newline style when saving files?
    – vtest
    Jun 25, 2010 at 5:39

9 Answers 9


There is:

  • 1
    This is the historically correct answer, though dos2unix is not always available these days.
    – Jared
    Jul 1, 2014 at 18:07

There is a Unix utility called conv that can convert line endings. It is often invoked with softlinks to u2d or d2u or unix2dos or dos2unix.

Additionally there are utilities called fromdos and todos.


Translate (tr) is available in all Unixes:

tr -d '\r'  # From \r\n line end (DOS/Windows), the \r will be removed so \n line end (Unix) remains.
  • 1
    Has a side-effect of removing any other \r's too. They're highly uncommon though. Jul 11, 2013 at 13:53

With sed and find that end with .txt, .php, .js, .css:

 sed -rie 's/\r\n/\n/' \
 $(find . -type f -iregex ".*\.\(txt\|php\|js\|css\)")

Doing this with POSIX is tricky:

  • POSIX Sed does not support \r or \15. Even if it did, the in place option -i is not POSIX

  • POSIX Awk does support \r and \15, however the -i inplace option is not POSIX

  • d2u and dos2unix are not POSIX utilities, but ex is

  • POSIX ex does not support \r, \15, \n or \12

To remove carriage returns:

awk 'BEGIN{RS="\1";ORS="";getline;gsub("\r","");print>ARGV[1]}' file

To add carriage returns:

awk 'BEGIN{RS="\1";ORS="";getline;gsub("\n","\r&");print>ARGV[1]}' file

Using man 1 ed (which edits files in-place without any previous backup - unlike: sed .. -i ".bak" ...):

ed -s file <<< $'H\ng/\r*$/s///\nwq'
  • I diden't even know ed,but it does the job (old but gold)
    – dwana
    Sep 9, 2015 at 14:01
  • This command worked for me when commands from other answers did not exist on the shared hosting I am using. Thanks :)
    – FK-
    Aug 18, 2019 at 10:45

Yes, use dos2unix. For example:

[justin@mybox ~]$ dos2unix myfile

$ recode dos.. FILE
$ flip -u FILE

(Each also exists for non-Ubuntu systems, but these links are handy.)


cat input.csv | sed 's/\r/\n/g' > output.csv

  • Some explanation and formatting would make this an answer. Right now it's just a comment. Sep 25, 2020 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.