How do I convert between Unix and Mac OS or Mac OS X text files?
Traditionally, Unix and Mac OS differ in the
format in which they store text files. Mac OS places a carriage
return character at the end of each line of a text file, but Unix uses
a line feed character. Some Unix applications won't recognize the
carriage returns added by Mac OS, and will display a file as a single
line, interspersed with Ctrl-m
characters. This appears
on the screen as ^M
. Similarly, some Mac OS applications
need to see carriage return characters at the ends of lines, and may
treat Unix-format files as one long line.
In Mac OS X, the situation is more complicated. Because Mac OS X is a meld of Unix and the older Mac OS, in some cases text files have carriage returns and in others they have line feeds. For the most part, classic applications still require text files to have carriage returns, while the command-line Unix utilities require line feeds. Mac OS X-native applications are frequently capable of interpreting both.
There are many ways to resolve the differences in format. In this
document you will find instructions on how to use Transmit,
MacSFTP, Fetch, command-line FTP,
tr
, awk, and Perl to do the
conversion. In Mac OS 9.x and earlier, before using these utilities,
the files you are converting must first be on a Unix computer, unless
you are using Transmit or Fetch, in which case the files must first be
on a Macintosh. In Mac OS X, you may use all of these methods from
your own computer; use Transmit, MacSFTP, and Fetch normally, and
command-line FTP, tr
, awk
, and Perl from the
Terminal application.
Transmit
When using Transmit to move a text file between Unix and Mac OS, be
sure to transfer the file in ASCII mode. This will ensure
that the document is transformed into a text format appropriate for
the host. Normally, Transmit will automatically decide what mode to
transfer a file in; to force it to transfer files as text, from the
File
menu, select ASCII Mode
.
MacSFTP
When using MacSFTP to move a text file between Unix and Mac OS, be
sure to translate the linefeeds. This will ensure that the document
is transformed into a text format appropriate for the host. To do
this, from the MacSFTP
(Mac OS X) or Edit
(Mac OS
9.x and earlier) menu, select Preferences...
. In the
preferences window, click the File Transfer
tab, then check
the box next to Translate text files linefeeds
. Click
Save
to apply the setting change.
Fetch
When using Fetch to move a text file between Unix and Mac OS, be sure
to transfer the file while Fetch is in Text mode. This will ensure
that the document is transformed into a text format appropriate for
the host. The format is specified by three buttons that appear in the
Fetch window after a connection has been made. To force a file to be
transferred in the Text mode, select Text
in the "Format:"
pop-up menu (Fetch 4) or click the Text
button (Fetch 3 and
earlier) before transferring.
Typically, Automatic
mode is selected by default; in this
mode, Fetch tries to determine whether a file is a binary
or text file by looking up the extension of the file in its suffix
mapping list. This list can be examined and modified by going to the
Customize
menu and selecting Suffix Mapping
. If
Fetch cannot find the file's extension in the list (or if the file has
no extension at all), it will default to either Text mode or Binary
mode, depending on the setting under the Customize
menu, in
Preferences
.
Command-line FTP
When using command-line FTP to move a text file between Unix and Mac OS, be sure to transfer the file in ASCII format. This will ensure that the document is transformed into a text format appropriate for the host. To do so, before you begin the file transfer, at the FTP prompt, enter:
asciitr
The Unix program tr
is used to translate between
two sets of characters. Characters specified in one set are converted
to the matching character in the second set. Thus, to convert the
Ctrl-m
of a Mac OS text file to the line feed
(Ctrl-j
) of a Unix text file, at the Unix command line,
enter:
Here, \r
and \n
are special escape sequences
that tr
interprets as Ctrl-m
(a carriage
return) and Ctrl-j
(a line feed), respectively. Thus, to
convert a Unix text file to a Mac OS text file, enter:
Note: The escape sequences must be surrounded by single quotation marks for these commands to work.
awk
To use awk
to convert a Mac OS file to Unix, at the Unix
prompt, enter:
To convert a Unix file to Mac OS using awk
, at the
command line, enter:
On some systems, the version of awk
may be old and not
include the function gsub
. If so, try the same command,
but replace awk
with gawk
or
nawk
.
Perl
To convert a Mac OS text file to a Unix text file using Perl, at the Unix shell prompt, enter:
perl -p -e 's/\r/\n/g' < macfile.txt > unixfile.txtTo convert from a Unix text file to a Mac OS text file with Perl, at the Unix shell prompt, enter:
perl -p -e 's/\n/\r/g' < unixfile.txt > macfile.txtNote: You must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside the quotation marks. At Indiana University, Perl is installed on all UITS shared central Unix systems.
Also see:
- When using FTP, why might programs and files fail to transfer properly?
- How do I convert between Unix and Windows text files?
- In Mac OS and Mac OS X, what should I do if Fetch isn't decoding downloaded BinHex, MacBinary, or StuffIt files?
- For Mac OS, what FTP clients are available and how do they compare?
- What are some common FTP commands?
- How do I convert a text file that I'm transferring between Mac OS or Mac OS X and Windows?
Last modified on May 15, 2005.