0

This question is directly related to another question I asked here.

Linux/Unix have nice utilities dos2unix and unix2dos for conversion of text files between the two OSs. Are there similar utilities for Macs and Linux?

2
  • As @Spiff said, you probably don't need anything Mac specific, as MacOS X is UNIX underneath. Are you using Mac OS 9 or previous? What version of MacOS/System do you need this for? Commented Dec 11, 2013 at 20:03
  • The old MacOS use \r as line break. The current version of Microsoft Excel of Mac still use \r as line break when saving a text file. For these files, run "mac2unix" or "dos2unix -c Mac " to convert the line break into \n.
    – Ben Lin
    Commented Apr 26, 2016 at 23:44

2 Answers 2

3

I don't know what dos2unix and unix2dos do exactly, but my guess is that it changes text file line endings from dos format to Unix format and back. Macs (as of OS X) and Unix use the same line-endings (line-feed, '\n') so if that's what you're worrying about, you don't need to do anything to move files back and forth.

Using dos2unix -c Mac mac_file turns the Mac formatted file into a Unix formatted file.

4
  • 1
    @octopusgrabbus I don't think -c mac is correct. I think that's a holdover from the 80's and 90's when classic Mac OS (System 1 through Mac OS 9) text files had bare CRs (carriage returns) as line endings). Mac OS X uses Unix line endings (bare LFs; linefeeds).
    – Spiff
    Commented Dec 11, 2013 at 19:59
  • @Eric be careful with \n. In many contexts, it's technically a "newline" which means that it will be replaced with the correct OS-dependent line ending sequence. So on Mac OS X and other Unixes, it will be replaced with an ASCII LF (linefeed) character 0x0a, but on Windows it may be replaced with a CRLF (0x0d0a). Back in classic Mac OS (Mac OS 9 and earlier), the \n sequence in MacPerl would be replaced by classic Mac OS's CR (just an ASCII 0x0d carriage return) line ending.
    – Spiff
    Commented Dec 11, 2013 at 20:03
  • I just tried dos2unix -c Mac on a Centos 6 system, and it turned the "Mac" file into a Linux file. That is it removed the <CR> and replaced it with an <LF> character. For a regular Windows file <CR><LF>, it stripped only the <CR>. Commented Dec 11, 2013 at 20:08
  • Whoever edited this to include -c Mac, Thank you. That's what I had to do to make it work. Commented Dec 13, 2013 at 19:32
1

OS X uses LF line endings and UTF-8-encoded files without a BOM in most places like other Unix platforms. CR line endings were mainly used in Mac OS 9 and earlier.

For example AppleScripts and text copied from some views in Finder and iTunes still use CR line endings. You can convert CR line endings to LF with mac2unix, dos2unix -c mac, or tr \\r \\n. dos2unix and mac2unix can be installed with brew install dos2unix.

If you need to convert Windows files to the format used by OS X, you can use dos2unix. It converts CRLF line endings to LF. If a file is encoded as UTF-16, it also converts the file to UTF-8, and if a file has a BOM, it removes it.

You must log in to answer this question.

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