Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I have set of files with either Windows newlines (CRLF) or Unix newlines (LF).

How can I convert all newlines in all files to Mac newlines (CR)?

share|improve this question

migrated from Nov 17 '10 at 0:53

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

I think you need to be more specific about what type of files you have. Are you trying to do this programmatically or just in your OS shell? – Guy Nov 16 '10 at 15:16
Do you want to do it on mac or on unix? – andcoz Nov 16 '10 at 15:20
I have hundreds of .c/.h files. I want to convert all those files to mac os format. I am ok with either ways, but my ultimate goal is to convert all those files to mac file format. – Thangaraj Nov 16 '10 at 15:21
it would be good if it is in mac. – Thangaraj Nov 16 '10 at 15:22
Search for 'dos2unix' on Voting to migrate. (And by the way, MacOSX systems don't use a different format than unix... you'd have to go back many years in Macintosh operating systems to find a time when they used a different format than MS/DOS and UNIX.) – Ether Nov 16 '10 at 15:43
up vote 2 down vote accepted

The question doesn't explicitly say, but I assume you mean text files, and need to convert the line delimiter format? OS X doesn't ship with command-line file converter tools, you have to build them yourself. Perl is good for Q&D utilities like this:

perl -pe 'if (s/\r?\n/\r/g) {$f=1}; if ($f&&eof()) {s/\r\z//}' PCfile.txt >Macfile.txt

or, to convert in place:

perl -pe 'if (s/\r?\n/\r/g) {$f=1}; if ($f&&eof()) {s/\r\z//}' -i convertfile.txt

Note: this script is a little more complicated than it probably needs to be, because it's written to work on on both PC-format (CRLF line terminators) and unix files (LF terminators), and leave files that're already in the old traditional MacOS format (CR separators between lines) alone. Also, the PC and unix formats put a terminator after the last line, while Mac format doesn't (it uses line separators, not terminators), so this script detects when it's actually translating, and removes the last delimiter.

share|improve this answer

CR line endings were mostly used in Mac OS 9 and earlier. OS X uses LF line endings in most places.

Some of the methods below add a newline to the end of files that don't already end with a newline. Some don't work on Windows or if the default line endings are not LF.


dos2unix *.txt
sed -i '' $'s/\r//' *.txt
tr -d '\r'


unix2dos *.txt
recode ../crlf *.txt

LF to CR:

unix2mac *.txt
awk 'BEGIN{ORS="\r"};1'
tr '\n' '\r'

CR to LF:

mac2unix *.txt
awk 'BEGIN{RS="\r"};1'

CRLF or CR to LF:

ruby -e 'print gets(nil).gsub /\r\n?/,"\n"'
ruby -i -pe 'BEGIN{$/="\r"};$_=$_.sub("\n","").sub("\r","\n")' *

CRLF or LF to CR:

ruby -i -ne 'print $_.chomp+"\r"' *.txt
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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