Convert a Text File to RTF, HTML, DOC, and more via Command Line

Jul 6, 2011 - 9 Comments

Terminal in mac OS

Do you need to convert a text file to RTF, plain text TXT, HTML, DOC, or another familiar document format? The excellent textutil command line utility can make quick work of text file conversion and manipulation on the Mac, and there is no need to download any additional software or tools as it’s built right in to Mac OS.

How to Convert Text File to RTF, HTML, DOC, etc from Mac Terminal

To begin your text conversion, you will need a starting text document that you wish to convert. It can be any text format if you’re testing this out, or use it with a document created entirely for this purpose.

Launch the Terminal application as found in /Applications/Utilities/ and then use the following command syntax:

textutil -convert filetype filename

Conversion options are txt, rtf, rtfd, html, doc, docx, odt, wordml, and webarchive, here’s sample syntax converting a text file named test.txt to an rtf:

textutil -convert rtf test.txt

Textutil will automatically append the filename with the appropriate extension. If you want to give the file a new name in the conversion process, use the -output flag as follows:

textutil -convert rtf test.txt -output NewFileName.rtf

If there’s multiple text documents you want to join together into a single new file, use textutil’s built-in cat function:

textutil -cat rtf file1.txt file2.txt file3.txt -output combinedFiles.rtf

You can quickly verify the conversion was successful by opening the newly created rtf file in TextEdit with:

open test.rtf

You can also go the other direction and convert from any of the aforementioned filetypes back to txt, just remember that plaintext does not support any styling so the document will be stripped of any unique fonts, font sizes, styling, or other aspects of a rich text file.

The command line tool textutil allows you to quickly convert text files to many other useful formats and perform other text manipulation tasks. Additional help and options for textutil can be found by summarizing the help tool, or referring to the man page:

textutil --help

Or to get the complete manual page on textutil:

man textutil

Don’t forget that you can also convert a text file to a spoken audio file, this can be achieved through the command line or by using the simpler “Add to iTunes as Spoken Track” method.

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

9 Comments

» Comments RSS Feed

  1. Ann says:

    In what is probably my root directory is the answer. Glad to have that sorted! This might help another user:
    Ann-Smiths-MacBook-Pro:~ tundern$ textutil -convert rtf Gugler.txt
    Ann-Smiths-MacBook-Pro:~ tundern$
    Where tundern is my user name.

    Cheers,
    Ann

  2. Ann says:

    Doesn’t work for me as Terminal keeps reporting that it cannot find the .txt file. I tried moving it from its nested folder to the Desktop with the same negative result. Where does Terminal expect to find the file?

  3. John says:

    Very useful note about using TextEdit to convert text to HTML thank you

  4. […] You can also open an RTF document into TextEdit and perform the conversion directly on the file, saving the resulting file as a TXT document. The same conversions can also be achieved from the temrinal by using the textutil command. […]

  5. Linds says:

    Here’s an Applescript droplet (OSX Lion) to package this up:

    (* ConvertFileFormat Β©Lindsay Crawford, 2012
    Based on William Pearson’s article https://osxdaily.com/2011/07/06/convert-a-text-file-to-rtf-html-doc-and-more-via-command-line/ and loosely, Apple’s Droplet with Settable Properties.app

    INSTRUCTIONS
    Run the app to convert one file, or drop a group of files on the app. Converted files with same names but different extensions are left in the original folder.
    Unconvertible files are ignored.
    *)

    property type_list : {“txt”, “rtf”, “rtfd”, “html”, “doc”, “docx”, “odt”, “wordml”, “webarchive”}

    on run — display description and get a file
    try
    open ({choose file})
    end try
    end run

    — This droplet processes files dropped onto the applet
    on open these_items

    set convertTo to (choose from list type_list with title “Format Conversion” with prompt “Select the format to convert the file(s) to:” default items {“html”} without multiple selections allowed) as text

    repeat with i from 1 to the count of these_items

    set this_item to item i of these_items

    — Get the info for the item
    tell application “System Events”
    set the item_info to (get the properties of this_item)

    set this_name to the name of the item_info
    set this_place to the POSIX path of the container of the item_info
    set ext to name extension of the item_info

    end tell

    — now check the file type
    if (ext is in type_list) then
    process_item(this_place, this_name, convertTo)
    end if

    end repeat

    end open

    — this sub-routine processes files
    on process_item(this_place, this_name, convertTo)
    — this_place is a POSIX path to the folder for this file
    — this_name is just the file name with extension
    — convertTo is a text string containing the new file extension

    copy “cd ” & quoted form of this_place & “;textutil -convert ” & convertTo & space & quoted form of this_name to theCommand
    do shell script theCommand

    end process_item

  6. D L S says:

    Loved it..

    I am a completely new and first Time Mac User and have been a Windows User for almost 10 years.. Just loved learning all this stuff..

    Used to do a lot of such things back when Learnt DOS in Windows

  7. […] addition to being able to convert TXT files to RTF or other filetypes, you can use textutil to convert the font family and font size of a document from the command […]

  8. david says:

    viceroy – you can do the concatenation by just using cat

    being a unix admin I love learning new tricks and can see potential benefits in certain cases by using this thanks!

    I think this goes hand and hand with being able to manipulate images and convert to different types try the following to learn more about that.
    sips –help

  9. Viceroy says:

    I like the concatenation aspect but I don’t think I would use the conversions when I can do it through other text editors.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site