Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Jun 25 '10 at 1:32

This question came from our site for professional and enthusiast programmers.

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

10 Answers 10

There is:

share|improve this answer
This is the historically correct answer, though dos2unix is not always available these days. – Jared Jul 1 '14 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.

share|improve this answer

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\)")
share|improve this answer

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'
share|improve this answer
I diden't even know ed,but it does the job (old but gold) – dwana Sep 9 '15 at 14:01

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.
share|improve this answer
Has a side-effect of removing any other \r's too. They're highly uncommon though. – ivan_pozdeev Jul 11 '13 at 13:53

Yes, use dos2unix. For example:

[justin@mybox ~]$ dos2unix myfile
share|improve this answer

You can use this to bulk replace all lines in multiple files that end with .html. You can change this to match a new name.

find -iname "*.html" -exec sh -cC '
sed 's/\r\n/\n/' "$1" > "$1"
' {} {} \;
share|improve this answer
This isn't safe! First, the output redirection will generally zero the files before sed gets to read their contents (if your sed has it, use -i instead of shell redirection to solve this). Second, that sed command will remove the last character of each line, whether or not it's a \r -- run it on a non-DOS-format file, and it'll delete part of the contents of the files. – Gordon Davisson Jun 24 '10 at 16:22

$ recode dos.. FILE
$ flip -u FILE

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

share|improve this answer

You can use Vim in Ex mode:

ex -bsc '%s/\r//|x' file
  1. % select all lines

  2. s substitute

  3. x save and close

share|improve this answer

I needed to convert line endings, as well as zip the file. The zip tool can do both at the same time:

zip --to-crlf LF.txt
zip --from-crlf CRLF.txt
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .