Convert various text file formats in the OS X Terminal with textutil

NewTerminalIconXThere are a number of ways you can convert a text document to another format, by simply opening it in a text editor like TextEdit and then choosing Save As from the File menu to export it. With TextEdit, you can choose Word, Rich Text, Plain Text, and OpenDocument Text, among others, as the formats in which to save your current file; however, if you are a Terminal user then you might enjoy knowing you can do this right from the command line.

One command-line tool Apple includes in OS X is “textutil” which can be used for a number of manipulations of supported text documents, with one of them being to convert a targeted document to a specified format:

  1. Open the Terminal
  2. Type the following command, replacing FORMAT with one of txt, html, rtf, rtfd, doc, docx, wordml, odt, or webarchive to specify the desired format:
    textutil -convert FORMAT
  3. Ensure there is a space after the format specification, and then drag your target document to the Terminal window, so the command looks something like this (in this case, converting a webarchive called “mypage” to docx):
    textutil -convert docx ~/Desktop/mypage.webarchive

When this command is executed, the resulting file will appear in the same folder as the original.

While the use of the Terminal for this might seem unnecessary given the ability to use various word processing programs for converting files, you can use it when managing text documents in automator routines, shell scripts, or applescripts where conversion of these documents might be desired.

One prime use for this routine is to batch-convert files, so if you have a folder of txt documents you would like to convert to docx, then you can do so by using Terminal wildcards with this command to target all or at least a group of desired files in the folder:

textutil -convert docx ~/Desktop/TextDocuments/*.txt

In the above command, any .txt documents in the folder called “TextDocuments” on the current user’s desktop will be converted to docx format.

The textutil command can be used for these format conversion routines, but also supports a number of other features such as specifying encoding, changing font sizes and type faces, and modifying file metadata.

6 thoughts on “Convert various text file formats in the OS X Terminal with textutil

  1. Chris Hart

    This is great to know. But I’m bummed that it doesn’t work with Pages documents. I recently ran into a situation where there was a need to batch convert Pages docs to Word docs. Thankfully, I found an AppleScript that would do the trick.

  2. MaX

    Pasting text into Terminal and copying it back also allows to make it truly plain text (to paste it elsewhere) if other tricks (pasting into Apple Mail, Apple TextEdit, Microsoft Word, Bare Bones BBEdit, etc) or tools like SmartWrap fail.

    1. Chris Hart

      Personally I paste stuff into Text Wrangler to quickly convert it to plain text.

      But this article primarily about doing the conversion as a batch, which the Terminal command above does nicely.

      Yes, pasting into the Terminal will strip formatting. But I think it’s ill-advised to tell the common to use the Terminal for such things. Those who aren’t familiar with it, don’t really belong in the Terminal. There’s too much potential for harm.

  3. AtumRa

    Thanks so much this was just what I needed to convert batches of .doc files to .txt. Too many companies wanting to charge $30 – $60 for a “converter” that probably just calls textutil. Every day I learn more about the command line utilities that are out there and expand my computer skills. Thanks again for the assist.

  4. rickla

    I’ve tried textutil several times. It does a great job of converting files to different formats or of concatenating multiple files into one. But I find it consistently fails to respect the -font or -fontsize parameter, making everything Times 12 whatever I enter for these parameters.

    Is that a bug, or am I just doing something wrong?

    (OS X 10.10.4)

Comments are closed.